Reputation: 3560
This is a conceptual question for me to understand C better. From what I understand about C arrays, they store pointers to values which are generated in sequence as per the space allocated. So for example, if I have this code
char someCharArray[] = {'a', 'b', 'c'};
Let's say my heap doesn't have sequential 12 bits for this array, so I store pointers to the following non-sequential addresses 0x001, 0x011, 0x040
.
I know two ways of accessing values from this array as shown in this answer.
Let's say I access the second element of the array like this
char datPointer = someCharArray[1];
In this case, datPointer
will point to 0x011
because the address is stored in the array.
However, wouldn't the following code return the wrong address?
char datWrongPointer = (someCharArray + 1);
Because I'm adding sequential addresses to the starting address of someCharArray, so assuming chars are 4 bits I'll get datWrongPointer
to be 0x004
instead of what it should be? Maybe I'm misunderstanding the addition function but I'd appreciate if someone could explain it to me.
Upvotes: 0
Views: 347
Reputation: 85867
From what I understand about C arrays, they store pointers to values
No, arrays store values directly. In fact, arrays essentially are their elements. C adds no additional structure.
char someCharArray[] = {'a', 'b', 'c'};
OK, here someCharArray
consists of 3 bytes of memory. If it lives in static storage (e.g. because it's a global variable), this memory is typically part of the data section; if it lives in automatic memory (i.e. it's a local variable), it's typically placed on the stack.
Let's say my heap doesn't have sequential 12 bits for this array
How is the heap involved here? Why 12 bits? C doesn't really support objects smaller than 1 byte (and bytes cannot be smaller than 8 bits).
Let's say for the sake of an example that someCharArray
ends up being placed at address 0xAB0000. Then the first element lives at 0xAB0000, the second element at 0xAB0001, and the third element at 0xAB0002.
char datPointer = someCharArray[1];
datPointer
is not a pointer, it's a single char
. This code will assign 'b'
to datPointer
(because someCharArray[1]
is 'b'
).
char datWrongPointer = (someCharArray + 1);
This is a type error.
someCharArray
evaluates to a pointer to its first element (as arrays do unless they're the operand of sizeof
or &
). This gives us a value of type char *
(and per above, the value is 0xAB0000).
We add 1, which adds enough bytes to get to the next element in memory. Here the pointer type is char *
, so we're adding 1 * sizeof (char)
(which is just 1 * 1
, which is just 1
) to the raw pointer value.
We end up with a char *
whose value is 0xAB0001.
We then try to assign a pointer to a plain char
, which is not valid. But we could dereference the pointer with *
and fetch the value stored there:
char x = *(someCharArray + 1); // 'b'
... or we could use a pointer variable:
char *ptr = someCharArray + 1; // 0xAB0001 in this example
Upvotes: 4