Created
January 27, 2020 17:27
-
-
Save jvoytech/214db4ad3481c1ec121f7722a3458753 to your computer and use it in GitHub Desktop.
table vs. pointer, differences
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
void F(int *x, int tab1[2], int tab2[]) { | |
printf("F: sizeof x, tab1, tab2: %ld %ld %ld\n", sizeof(x), sizeof(tab1), sizeof(tab2)); | |
} | |
int main() { | |
int tab[] = {1,2,3,4,5}; | |
int* x = tab; | |
printf(" x=%p, &x=%p, *x=%d, x[0]=%d\n", x, &x, *x, x[0]); | |
printf("tab=%p, &tab=%p, *tab=%d, tab[0]=%d\n", tab, &tab, *tab, tab[0]); | |
printf(" x[1]=%d, *(x+1)=%d\n", x[1], *(x+1)); | |
printf("tab[1]=%d, *(tab+1)=%d\n", tab[1], *(tab+1)); | |
printf("sizeof tab[]: %d\n", sizeof(tab)); | |
F(x, tab, tab); | |
return 0; | |
} | |
/* | |
Output: | |
x=0x7ffe07c01e20, &x=0x7ffe07c01e18, *x=1, x[0]=1 | |
tab=0x7ffe07c01e20, &tab=0x7ffe07c01e20, *tab=1, tab[0]=1 | |
x[1]=2, *(x+1)=2 | |
tab[1]=2, *(tab+1)=2 | |
sizeof tab[]: 20 | |
F: sizeof x, tab1, tab2: 8 8 8 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment