0

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.

rassa45
  • 3,482
  • 1
  • 29
  • 43
  • 3
    "*From what I understand about C arrays, they store pointers to values*" <- no, they just store values :o and the code following looks **very** wrong :o –  May 02 '18 at 16:22
  • 1
    *From what I understand about C arrays, they store pointers to values which are generated in sequence as per the space allocated* - hm. No. Arrays is just a **contiguous** memory region where values are stored sequentially. – Eugene Sh. May 02 '18 at 16:23
  • Lol. Can you add an answer that explains it better? I've been reading a few Unix and C books and it's just not clicking. There was a point today when I thought the dereference `*` was overloaded and sometimes return pointers and sometimes didn't, so I'm lost. – rassa45 May 02 '18 at 16:24
  • I would add an answer if I fully understood what you are trying to do .... –  May 02 '18 at 16:25
  • Forget about pointers for a moment. They are irrelevant for (this) array discussion. – Eugene Sh. May 02 '18 at 16:25

1 Answers1

4

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
melpomene
  • 84,125
  • 8
  • 85
  • 148
  • Changed my question to reflect a proper initialization. If arrays store values and not pointers as I understood, do they have to allocate all their memory at once (don't know where this would be, I thought this was allocated but heap but maybe I'm wrong), so essentially arrays are pointers to a large chunk of memory that contains smaller subsets of data? – rassa45 May 02 '18 at 16:34
  • 1
    @ytpillai Arrays are allocated all at once, like all other types. Arrays are not pointers. Arrays are a collection of elements placed contiguously in memory, plus some weird conversion rules (almost all uses of an array in C code end up implicitly converting the array to a pointer to its first element ("pointer decay")). – melpomene May 02 '18 at 16:44
  • @ytpillai ... PS: If you see any tutorial talking about "the name of an array", throw it away. Names don't matter. – melpomene May 02 '18 at 16:46
  • Your answer is the best I've ever seen. Thanks so much for the work you out into it, I can finally get past that whole confusion. I've accepted but I think if you put it somewhere more accessible than this question as well you can really help a lot of people. – rassa45 May 02 '18 at 18:49