It looks like you're not confused at all. Please provide some examples of what you don't understand.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
consider example given by asitmahato.
int b[10]
scanf("%d",b);//b is also the base address of the array .
It is same as writing:
int b[10]
scanf("%d",&b);//gives 'address of' b,(the zeroth place)
printf("%d\n",*(arr));//prints the 'value at address" zero of array b
No, it's not the same. The address passed to scanf() will be the same, but the type of that object is different. It's the difference between a pointer to char and a pointer to an array of char. The latter is undefined behavior because it's not the expected type for the %d format specifier. This is avery subtle error that rarely manifests as a real problem, so beginners tend to have a hard time understanding it.
In value context, array names are converted to a pointer to the first element (ie. b in the above example is converted to &b[0] ). The reason we use the address-of operator (&) in scanf() is to get a pointer to the object so that scanf() can modify it through indirection. Since the array name is already a pointer, there's no need for another level of indirection.
Of course, this whole issue with arrays can be sidestepped by being explicit:
int b[10];
scanf("%d", &b[0]);
But that still doesn't help if you have a pointer to a scalar:
int *p = malloc(sizeof *p);
scanf("%d", p); /* No '&' necessary */
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Why don't they(those who standardize C and even the compiler manufacturers) take care of this 'undefined behavior' thing.
How would they take care of it?
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401