last day (15 days later) » 

3:12 PM
0
Q: dereference pointer to an array

MilanI have a basic doubt, when we de-reference a pointer to an array why we get name of the array instead the value of first member of an array, I see this has been asked here but I didn't get it exactly how? dereferencing pointer to integer array main() { int var[10] = {1,2,3,4,5,6,7,8,9,10}; ...

 
Milan, Shouldn't *ptr print 1 --> ptr is a pointer to an array. *ptr is an array. An array is not an int 1.
 
@chux-ReinstateMonica, I am just confused how *ptr is decoded to an array, I understand ptr is a pointer to an array. how *(&a) gives an array instead of value 1. At address &a value stored is 1.
 
@Milan At address &a value stored is also the array (40 bytes long).
 
and that is what I failed to understand, how :(
 
@Milan Consider another case. If b was a pointer to a widget, would you expect *b to be a widget or an int?
 
3:12 PM
it should be an integer value.
@chux, is that wrong ?
 
No. A widget, some arbitrary data type, should not be an integer value.
It could be a string, a double, a struct combining many types or an array if int, etc. as in your case of ptr.
"At address &a value stored is 1." is in incomplete conclusion. True that an address points to a single byte - the beginning of an n-byte object. The object is not just that one byte, but potential more, like 4 bytes for an int, 40 for an int array of 10, etc.
 
okay, *b in that case is widget then. I am *not able to just understand how, *ptr has type int[10]? ptr is pointer to array of 3 integer , when we *ptr then it is pointing to first element of the array that is of type int
 
"when we *ptr then it is pointing to first element of the array that is of type int" is amiss.
*ptr is an array, not a pointer.
Depending on how *ptr is used, further conversions can occur.
 
3:28 PM
is there a way to visualize this , sorry if I am really troubling you.
 
In your code, *ptr is passed as a function argument. It is then converted to the address and pointer type to the first element. IN other cases like sizeof *ptr, there is no such conversion.
Of course there is a way to visualize albeit a chat does not well support that.
So printf("value = %TBD\n",*ptr); needs a specifier to go with the address of an int.
C does not have a specifier for that.
So instead, convert that int * to a void * and use %p
printf("value = %p\n",*(void*) *ptr);
To print the value of var[0], use printf("value = %d\n",**ptr); or printf("value = %d\n",var[0]); or printf("value = %d\n", *var);.
or printf("value = %d\n",(*p)[0]);
printf("value = %p\n",*(void*) *ptr); is a typo. S/B printf("value = %p\n", (void*) *ptr);
GTG, I'll check back later.
 
3:56 PM
Thanks for your time. I think , with one of your statement I see the point now. You wrote "ptr is an array, not a pointer". ptr is pointer to an array and dereference the ptr will give us an array which is decayed into address of the first element. is this right understanding ?
 
4:32 PM
[You wrote "ptr is an array, not a pointer".] --> I wrote [*ptr is an array, not a pointer.] -do not forget that *. [dereference the ptr will give us an array which is decayed into address of the first element] is more like [dereference the ptr will give us an array which is decayed into address of the first element in some contexts] like passing as a function argument.
 
yeah sorry. right its *ptr is an array, and size of for *ptr still give entire array size. Also, is it right when one say int a[10], here a is array of type int[10] ?
 
5:17 PM
int a[10] best as array 10 of int.
 

last day (15 days later) »