scanf("%d",b);//b is also the base address of the array .
It is same as writing:
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 a very 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 ). 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:
But that still doesn't help if you have a pointer to a scalar:
int *p = malloc(sizeof *p);
scanf("%d", p); /* No '&' necessary */
I was only discussing language specifications not implementations but I should admit I didn't know about the 'undefined behavior' in the above case.I'm glad you clarified what to use and what to not.
This is a very subtle error that rarely manifests as a real problem, so beginners tend to have a hard time understanding it.
Not if they bang their heads more often.Am I right?
The latter is undefined behavior because it's not the expected type for the %d format specifier.
But I don't get it. Why don't they(those who standardize C and even the compiler manufacturers) take care of this 'undefined behavior' thing. I guess this is one of the main reasons why C is hard to grasp and is even considered as a "dangerous programming language".
#define A 3// name a constant
long int booking_ID ; ...