I saw a weird type of program here.
int main()
{
int s[]={3,6,9,12,18};
int* p=+s;
}
Above program tested on GCC and Clang compilers and working fine on both compilers.
I curious to know, What does int* p=+s;
do?
Is array s
decayed to pointer type?
s
can be safely omitted, it has no effect – Livelysizeof
an array and a pointer. – Armisteadint b[2]; int a* = b;
thenint *c = b+1
andint *d = a + 1
will contain different values – Ajitint* p = &s[0]
. But that's me. Actually, I would usestd::array
... – Arthrospore+=
operator originally was=+
, which was changed presumably exactly because of the ambiguity with the unary+
. – Fumikofumitoryb+1
b
decays to a pointer to its first element, i.e. is equal in type and value toa
. If you want to point out (heh) the esistence of real arrays you could do something likeint *p = b; int (*pa)[2] = &b; cout << "p: " << p << "pa: " << pa << "p+1: " << p+1 << "pa+1: " << pa+1 << endl;
which should yield equal values forp
andpa
but different ones for the respective increments (becausepa
would point to the next array if there were one). – Fumikofumitorysizeof
to a dynamically allocated array because dynamically allocated array does not have a name. Also, this question is about C++, not about C. In C unary+
is not applicable in this fashion, which would prevent the code from compiling. The question is exclusively about C++. – Irrigationint* p = std::decay(s);
– Cunninghamstd::decay_t<int>()
? – Concert