r/cprogramming • u/sudheerpaaniyur • Dec 22 '25
pointer, decay, past array confusion
`int myarray[4]={1,3,5,7,};`
`int *ptr, *ptr1;`
`ptr=&myarray + 1;`
`ptr1=*(&myarray + 1);`my confusion: I am not understanding how ptr and ptr1 is same, in my understanding & is adress and * is used for derefercing, but in ptr1 have both here i got confuse.
what is decay here?
10
Upvotes
9
u/cursed-stranger Dec 22 '25 edited Dec 22 '25
Didn't you get compilation errors from that? What compiler are you using?
3
u/Educational-Paper-75 Dec 22 '25
Although you do not (ever) need to use the address-of unary operator on an array the compiler may well ignore it. For all practical purposes best remember that an array (name) ‘is’ (similar to) a memory address, although that’s assigned by the compiler.
2
u/[deleted] Dec 22 '25 edited Dec 22 '25
myarrayis of typeint[4]int[4]decays toint*, which is the type ofptr. Thus you can assignmyarraytoptr.So far so good.
However, you have a compile error for the line
ptr = &myarray + 1,&myarray + 1doesn't decay to anint*. It is of typeint (*)[4]. You need to remove the&so that pointer decay can kick in and add offsets. To make&myarray + 1assignable toptr, you'll have to change the declaration toint (*ptr)[4] = &myarray + 1;Let's break down the last assignment
ptr = *(&myarray + 1);&myarrayis of typeint (*)[4]from above, basically a pointer to an array of 4 ints. By adding 1 to this expression, you're referring to the next array of 4 ints, not the second element in the array.2
u/SmokeMuch7356 Dec 22 '25
Strap in, this is going to get bumpy.
Unless it is the operand of the
sizeof,typeof, or unary&operators, or is a string literal used to initialize a character array in a declaration, an expression of type "N-element array ofT" will be converted, or "decay" to an expression of type "pointer toT" and the value of the expression will be the address of the first element.In other words, any time the compiler sees the expression
myarrayin your code, it replaces it with something equivalent to&myarray[0]unlessmyarrayis the operand ofsizeof,typeof, or unary&.The expression
&myarrayhas typeint (*)[4](pointer to 4-element array ofint). The expression&myarray + 1yields a pointer to the next 4-element array ofintfollowingmyarray, and again its type isint (*)[4].int *andint (*)[4]are not compatible types, soshould have resulted in a diagnostic.
However, in the statement
the
*dereferences&myarray + 1, which gives us a type ofint [4], which then decays toint *, so the types match up. Unfortunately, since&myarray + 1yields an address that isn't part of the array, the behavior on dereferencing it is undefined.Picture (addresses are for illustration only):
Again, the behavior on trying to read or write
myarray[4],*(myarray + 4), or*(&myarray + 1)is undefined. It may work, it may crash, it may start mining bitcoin.4
u/dcpugalaxy Dec 22 '25
When you use the name
myarrayas an rvalue it is converted from aT[4]to aT*and specifically to a pointer to its first element, as if you had written&myarray[0].In the expression
&myarray, myarray is not used as an rvalue as it is the operand of the addressof operator (&). As such, you get out a pointer to an array: it is of type pointer-to-array4-of-int. You then add 1 to that, which gives you a pointer immediately after myarray (which is not if much use). It is invalid to dereference that pointer as you do on the next line, but when you do the type of the result isint[4](array4-of-int) which implicitly converts toint*, albeit to an invalid object.When you set
ptr, you are assigning from one type of pointer to another. This is allowed but generally a bad idea and if you turn on compiler warnings it will tell you not to do so.If you get rid of the
&then your code is closer to being correct.ptr = myarray + 1is just the same as getting a pointer to the second element of your array (the 3) and*(myarray + 1)is identical tomyarray[1]which is 3. But you would need to assign that to an int variable not to ptr1 which is a pointer.That is because in
myarray + 1, myarray is used as an rvalue, so it decays into a pointer. In&myarray, it is used as an lvalue, and then you take the address of that lvalue ie. you get a pointer to the lvalue ie. you get a pointer to the array. That is conceptually different to getting a pointer to the first element of the array, which is what array to pointer decay produces.