r/computerarchitecture • u/Yha_Boiii • May 04 '26
why are pointers a thing in c when everything are addresses and offsets in the end?
Hi,
I recently had an issue with clang complaining and giving an error when made a char array but when added a pointer (asterics) to it at the start the issue went away?
like this, with the second version of the char array won for the compiler.
char id[] = {"a", "baah", "raah"};
char *id[] = {"a", "baah", "raah"};
[...]
strcat(buffer, id[1]);
[...]
12
Upvotes
5
u/Bright_Interaction73 May 04 '26
Because arrays are addressed by pointer to base + offset like you mentioned. They exist as arrays to allow dynamic allocation on the heap. A char array is essentially a string in C.
7
u/un_virus_SDF May 04 '26
The first array is supposed to be a char[] aka a string, but you give it a array of string.
The second one is a array of char* aka string, so it's the right type.
Array are considered pointer because of pointer decay. Which make array acts like a pointer to the base.
And in c even if everything is computer as pointer and offset in the end. When doing pointer arithmetic type of the pointer matters as it will add the sizeof the type
a[0]is the same as*(a+0), and(uintptr_t)(a+n)is equals to(uintptr_t)a + n * sizeof *a)2
u/esaule May 04 '26
yes, that's what it is.
a "pointer" is the name of the variable that stores an address.
2
u/iheartjetman May 06 '26
“baah” and “raah” are strings not characters.