I’m a beginner in C and I’m trying to understand the concept of pointer arithmetic:
I have a code like this :
#include<stdio.h>
void main(){
int a[10];
if(a)
printf("%d\n",*a);
}
Which prints the address of first element in array a. That’s fine. But in my printf statement I’m using the * operator to print the value.
But when I look at my if statement, I wonder how without * operator, if is working on a?
I mean without * operator, how the if statement accesses the object the pointer points to?
I guess i’m clear enough about my doubt, thanks in advance.
In your code
*ais equivalent witha[0]. You’re not printing any address, just some uninitialized value.EDIT as per comment:
In your code
if (a)doesn’t access the contents, it only tests the address of a – which will never evaluate to 0.