-1

So, I've just learnt about arrays, pointers and decaying but I don't really understand.

If "arrays" (the things declared with int arr[]) only decay to pointers when passed to functions, why does an array equal a pointer to its first element when evaluated in main? Furthermore, if "arrays" in main are not pointers, but arr[x] is syntactic sugar for *(arr + x), why is the dereferencing required if the "array" is not a pointer. Adding to my confusion is the fact that the sizeof(arr) in main returns the correct value, while in a function it returns the size of the pointer. Furthermore, the CS course I am taking stated that the name of an "array" is actually a pointer to its first element, however again, this is not what I have heard from other sources.

Also, if "arrays" are actually pointers, is there any way to directly create an array, skipping the intermediate pointer (if you would even want to do such a thing).

Can anyone help clear all this up for me?

saije
  • 1
  • Arrays decay into pointers in almost every context, not just when when passed to functions. See https://stackoverflow.com/q/17752978/ – Nemo Jan 26 '20 at 05:56
  • Does this answer your question? [What is array decaying?](https://stackoverflow.com/questions/1461432/what-is-array-decaying) – Imran Ali Jan 26 '20 at 06:01
  • 1
    "_if 'arrays' are actually pointers_" -- arrays are _not_ pointers; this is illustrated by the observation that you found confusing: `sizeof arr` yields the size of `arr` in bytes when `arr` is an array. This is because arrays do not decay when they are operands of `sizeof`. – ad absurdum Jan 26 '20 at 06:30
  • 1
    Arrays are not pointers. Array objects do not store a pointer value anywhere. *Expressions* of array type (`T [N]`) are implicitly converted (“decay”) to pointer types (`T *`) *unless* the array expression is the operand of the `sizeof` or unary `&` operators, or is a string literal used to initialize a character array in a declaration. C was derived from an earlier language called B, which *did* maintain a separate pointer to the first element, so `a[i] == *(a + i)`. Ritchie wanted to keep B’s array semantics without having to store that pointer, hence the decay rule. – John Bode Jan 26 '20 at 12:42

1 Answers1

-1

Firstly array's name is considerd to be a pointer. @Sowmya is right about all the thing about train and compartment that array's name is the first thing and then system calculates all other elements on the behalf of the array's name that is the address of the first element. when declared an array like

int arr[10];

then arr is a kind of pointer.that points to the array of 10 numbers another thing is it's an address so you can also do *(a+11) that is accessing the 11 the element which is in our case will not give a compile time error but may give run time error as Segmentation Fault or may give garbage value. so what I conclude is the name of the array is a pointer. Now the decaying thing-- when you do sizeof(arr) it gives you the size of array that is (if int is 4 byte) then sizeof will give 40. but when you pass the array to a function it decays into a pointer it is no longer an array but as we know we can access elements by simply dereferencing them.So it still works but sizeof will understand arr to be a pointer and will give the size of a pointer(let's say in the system size of a pointer is 8 byte) that is 8 byte.

Namit Piriya
  • 141
  • 7
  • 1
    `arr` is not considered a pointer. It does not store a pointer value. It stores the elements of the array *and nothing else*. When it appears in most contexts, the compiler *converts* the array expression to a pointer to the first element. But the array object itself is not a pointer, nor does it contain any kind of pointer within it. – John Bode Jan 26 '20 at 12:46
  • @John Bode I understand that arr is not considered as a pointer but what we learnt is arr refer to the 1st element of the array that's why I said it's kind of pointer. – Namit Piriya Jan 26 '20 at 13:55
  • "_Firstly array's name is considered to be a pointer._" "_`arr` is a kind of pointer._" "_`arr` refer to the 1st element of the array._" -- not one of these statements is true. Loosely, arrays decay to pointers in _most_ (but not all expressions); this does not mean that arrays are pointers (or kinds of pointers). OP has confusion about arrays, and so does this answer. – ad absurdum Jan 26 '20 at 15:36