This may be a very basic question but the idea of having pointers in C seems confusing to me or may be I don't know the exact purpose. I will provide some examples to demonstrate what my concerns are:
1st point:
Definition says something like this:
A pointer is a variable containing the address of another variable.
So, if one program goes like this:
int i = 23;
printf("%d", i);
printf("%d", &i);
And, another program goes like this:
int i = 23;
int *ptr;
ptr = &i;
printf("%d", *ptr);
printf("%d", ptr);
Both the programs above can output same thing.
If pointer also keeps the variable's address in it and at the same time we can get the variable's address using &
sign, can't we do the same task pointer does by deriving the address of any variable? I mean if I don't declare it as pointer and use it as int ptr = &i;
in 2nd code snippet and use it as normal variable, what would be the differences?
2nd point:
I found somewhere here that:
C does not have array variables....but this is really just working with pointers with an alternative syntax.
Is that statement correct? As I am still beginner, I can't validate any statement regarding this. But this was somewhat confusing to me. If that statement is correct either, then what is the actual workaround in this regard? Is it actually the pointers which works in back-end and just the compilers/ide are fooling us by using array (obviously for maintaining simplicity)?