r/C_Programming • u/Yelebear • Sep 14 '26
Question Question about arrays (beginner)
From what I understand, behind all the magic, array is just a variable that points to a memory address.
The difference is that we are also creating extra values in stack.
So when we create an array with a [], we're just telling the computer to make these x amount of variables but make sure they are right next to each other in sequence, no gaps in the memory address.
And when we call for an array value with an index, we're just counting that many steps forward in the memory address.
When we say array[] = {5,7,3}, it's really just *array = 5.
Did I get it wrong? Did I miss something?
Thanks for the quick replies. I'm currently learning about Pass by reference.
39
u/bayesianparoxism Sep 14 '26
int a[5] = {10, 1, 0, -5, 30};*a == 10and*(a+3) == a[3] == -525
u/thank_burdell Sep 14 '26
Close. The array initialization is putting the 5, 7, and 3 into those contiguous memory addresses at whatever size you declared for the array. *(array+1) equals 7.
6
u/Turbulent_File3904 Sep 14 '26
no, array is different from pointer. arrays are continous collection of object having same type, while pointer are address by definition they are not the same.
in most context except when use sizeof or alignof operator otherwise array object used in expression decay into address pointing to first element in array. example sizeof(int[5]) yields 20 on my machine, while sizeof(int *) yields 8
and array objects like any other object can have different storage(static, automatic, or dynamic allocated)
you can actually create a pointer to array: int (*pa)[5] is a pointer to array of 5 elements, while int *p; is a pointer to an int or an array.
if someone say array and pointer are the same. dont learn from them, period.
5
u/SmokeMuch7356 Sep 14 '26 edited Sep 14 '26
When you declare an array:
what you get in memory is:
That's it. No storage is set aside for a pointer, or array size, or anything else. You just get a sequence of objects of some type.
Under most circumstances an array expression will be converted to a pointer to the first element; IOW, when the compiler sees the expression
a, it replaces it with something equivalent to&a[0]. So when you write things like:the compiler basically emits code like
Why, though?
C was derived from Ken Thompson's B programming language. In B, when you created an array, an extra word was set aside to store the address of the first element:
The array subscript operation
a[i]was defined as*(a + i); offsetiwords from the address stored inaand dereference the result.Ritchie wanted to keep B's array behavior, but he didn't want to set aside space for the pointer. So he created the rule that array expressions "decay" to pointers.
a[i]is still defined as*(a + i), but instead of storing a pointer,aevaluates to a pointer. However, you can use the[]on regular pointers too:The only exceptions to the decay rule occur when the array expression is the operand of the
sizeof,typeof, and unary&operators, or when the expression is a string literal being used to initialize a character array in a declaration.8
u/Eidolon_2003 Sep 14 '26
Yes about it being a pointer to the beginning of the array and that indexing is just adding an offset to the pointer. However, arrays don't necessarily live on the stack. Depending on how you allocate the memory, it could be on the stack, in the heap, or in the global data segment.
3
u/zhivago Sep 14 '26
Note that there is no pass by reference in C.
1
u/sciencekm Sep 14 '26
Let's say you have somewhere in memory (lets call it address 3000) a sequence of bytes stored there, it would look like this:
3000: 'x'
3001: 'y'
3002: 'z'
Instead of using 3000 to access this memory, you call it array A.
Now let's say you have somewhere in memory (lets call it address 4000) a value of 3000. We say that this is a pointer to 3000. We can call this 4000 as P.
4000: 3000
You can change the value stored in 4000 so that it "points" to somewhere else, say memory 9000, because maybe there is another array of bytes there that you want to access.
Now lets say you have somewhere in memory (lest call it address 5000) a value of 4000. We say that this is a pointer to P or a pointer to 4000 (a pointer to a pointer). We can call this 5000 as Q.
1
u/sciencekm Sep 14 '26 edited Sep 14 '26
The difference between an array and a pointer is more than just semantics - it is 100% technical. Here is a very, very simple demonstration.
Given the following source:
It does not matter what optimization you use, you will always get:
Array access is always a 1-step process. Pointer access is always a 2-step process.
Line 5 - get arr[0] from memory
Line 6 - first get the value of the pointer, then use that to get ptr[0] from memory
1
u/Key_River7180 Sep 14 '26
Arrays are contigious values that the C compiler indexes using a formula:
*arr + (sizeof(*arr) * Index)
Imagine memory as a neighbourhood, each house has an address, and what is inside is the value. An array is just a set of contigious houses, starting in an adress and ending in another. When we say arr[6], we tell C to give us what is inside house 6 starting from an adress.
But we measure everything in meters for some reason, so It really just tells you what is inside the house at meter x.
1
u/Duck_Devs Sep 15 '26
For purposes other than sizeof and other static expressions, the name of an array evaluates to a pointer of its first element.
For instance, the following two pieces of code do the same thing.
int arr[] = {5, 7, 3};
int *pointerToFirst = arr;
int x = 5, y = 7, z = 3;
int *pointerToFirst = &x;
I’m not a C expert by any means, so if I said something inaccurate please correct me, but this is the easiest way to think about it IMO.
1
u/tottasanorotta Sep 15 '26
Conceptually yes. The difference really is that the array provides additional information to the compiler about how many elements it contains. So you can use sizeof(array) to get the total size in bytes that the array contains, because it knows how many elements it has and what type they are. With a pointer you can't do that unless you explicitly store another length variable, because it only knows a memory address and a type.
1
u/mredding Sep 15 '26
You speak of it as though C were a high level assembly, and it is not. The language expresses axioms and propositions that it can prove - and thus guarantee, when the program compiles; proofs that are LOST in the transform to machine code, but the machine code could not be generated without the proof. Compilation is actually a hugely lossy process, and the consequences leave the original C behind.
So arrays in C are distinct from the machine code generated from them as a consequence. It's worth really understanding this so you can really start grasping what you can express with a programming language.
1
u/flyingron Sep 14 '26
You understand WRONG. An array name isn't any different than any other variable. It not only knows where the object is in memory but also it's type and size. It's NOT just a pointer (though it implicitly converts to one for hysterical reasons).
When you declare an array: T t[10]; It says stick 10 t's together and treat them as one thing.
The other funky thing in C is you can't apply the [] operator to arrays. They only work with pointers, but you get a free conversion to pointer. t[5] says, convert t to pointer to the T that is the first element of the array and then go 5 things further along in memory and use that.
t[x] is exactly the same as *(x+t). You can even say: 5[t] if you want.
1
u/SwordsAndElectrons Sep 14 '26
More or less, but you omitted the other two elements.
For the most part, the indexed syntax of an array is just semantic sugar. A slightly nicer way of being able to access a contiguous block of elements than regular pointer arithmetic.
1
u/llynglas Sep 14 '26
Dead right....
1
u/zhivago Sep 14 '26
It's a common point of confusion.
This question may help you clear it up.
Given char c[3]; what is the type of c?
0
u/duane11583 Sep 14 '26
arrays and pointers are almost the same thing.
the name of the array is the location in memory where the array starts.
a pointer is the location in memory where the item is stored.
its not uncommon to have an quasi-array-of-length-one, which is effectively a pointer
0
u/RRumpleTeazzer Sep 14 '26
the array makes space for multiple elements. you get a pointer to the first element, and you can use the [index] syntax to dereference the index'th position for reading and writing.
when you do array[2], yoh effectively do *(array + 2).