From what I understood, I am passing the address of the variable a
to the function int ffx1
.
After that, what exactly does this line p = (int[2]){*p};
mean?
int ffx1(int * p)
{
p = (int[2]){*p};
return(p[1]);
}
int main()
{
int a = 1;
a = ffx1(&a);
printf("%d", a);
return 0;
}
With int ffx1(int * p)
, p
is a pointer to an int
.
(int[2]){*p}
is a compound literal defining an int
array of 2. @BLUEPIXY This unnamed array we will call X[2]
. Its first element have the value of *p
, and the next element, being an int
will be initialized to zero (§6.7.9 21 & 10 ) as it is not explicitly listed.
p = ....
assigned the pointer p
to a new value. In this case, the above array X[]
is converted to the address of its first enrollment or &X[0]
.
return p[1]
de-references p
, returning the value in X[1]
or 0
.
It is a pointer to compound literals.
C11-§6.5.2.5 Compound literals (p9):
EXAMPLE 2 In contrast, in
void f(void) { int *p; /*...*/ p = (int [2]){*p}; /*...*/ }
p is assigned the address of the first element of an array of two ints, the first having the value previously pointed to by p and the second, zero. The expressions in this compound literal need not be constant. The unnamed object has automatic storage duration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With