For the following code fragment:
unsigned int *ptr[10];
int a[10]={0,1,2,3,4,5,6,7,8,9};
*ptr=a;
printf("%u %u",ptr,a);
i checked on codepad.org and ideone.com.On both compilers its showing different values of ptr and a
For the following code fragment:
unsigned int *ptr[10];
int a[10]={0,1,2,3,4,5,6,7,8,9};
*ptr=a;
printf("%u %u",ptr,a);
i checked on codepad.org and ideone.com.On both compilers its showing different values of ptr and a
With warnings on:
pointer targets in assignment differ in signedness
format ‘%u’ expects type ‘unsigned int’, but argument 2 has type ‘unsigned int **’
format ‘%u’ expects type ‘unsigned int’, but argument 3 has type ‘int *’
When used in pointer context, ptr
points to the beginning of ptr
array, while a
points to the beginning of a
array. These are two different arrays that occupy completely different places in memory. Why would they be the same?
Of course, printing pointer values with %u
is a crime. Use %p
. That's what %p
is for.
This is a array of pointers
*ptr[10]
If you want to assign a to this go:
(*ptr)[10]
uint *ptr[10]
is equivalent to uint **ptr
and assignment *ptr = a
is the same as ptr[0] = a
which assign a
to first offset inside ptr
array, it doesn't touch value of ptr
itself...
You maybe wanted to used one of these:
ptr = &a;
// Or
printf("%u %u",ptr[0],a);
// Or
unsigned int *ptr;
ptr = a;