What is the meaning of "p = (int[2]){*p};" line in C pointer?
Asked Answered
S

2

10

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;
}
Sylvan answered 31/10, 2017 at 17:36 Comment(10)
(int[2]){*p} is compound literal. p = (int[2]){*p}; such as int array[2] = { *p, 0 }; p = array;Deipnosophist
if it is a compound literal populating the array, it's not specifying a value for the 2nd element of the array, which is the one that is returned which could be UBNorseman
@AndyG Yes it is, see BLUEPIXY's comment.Suspender
@coderredoc LifetimeDiversification
@coderredoc Although: C11 draft standard n1570: 6.5.2.5 Compound literals 5 The value of the compound literal is that of an unnamed object initialized by the initializer list. If the compound literal occurs outside the body of a function, the object has static storage duration; otherwise, it has automatic storage duration associated with the enclosing block.Diversification
@BLUEPIXY.:If possible Write this out,,,this is good to discuss?Armalla
@EOF Why is Lifetime a problem?Deipnosophist
@Deipnosophist It isn't. I was speculating about why somebody would think this was UBDiversification
@EOF ah, by the way I do not think this is UB.Deipnosophist
@Deipnosophist I agree.Diversification
C
9

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.

Cannes answered 31/10, 2017 at 18:10 Comment(0)
B
12

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.

Bewail answered 31/10, 2017 at 17:50 Comment(6)
Thanks. your answer is right.. But since it is mentioned p[1] and int[2] then what is the length of the unnamed array? 2 or 3?Sylvan
@Sylvan The unnamed array, called X[] in this answer, consists of 2 elements because of the (int[2]).Bewail
Thanks. Good explanation. but here instead of writing p = (int[2]){*p}; can I write int *q = (int[2]){*p}; then what should be my return statement? return p[1] or q[1]? I guess q[1] rightSylvan
@Sylvan Yes you can write int *q = (int[2]){*p} to avoid changing the local variable p. return q[1] would work like above. return p[1] would work with the original p passed in which does not pointer to 2 elements.Bewail
Why did you mention "all zero bit pattern" ? It happens to be true in this case, since int is defined as having all zero bit pattern for zero value; but if let's say it were an array of pointers, then the other entry gets the null pointer -- not necessarily all zero. The other entries are initialized as if by {0}Daman
@Daman "Why did you mention "all zero bit pattern" --> Because I recalled it as that the memory will be filled with all zero bits as in calloc(). Yet non-explicit initialization in general is as you say - potentially not an all zero bit pattern - answer amended.Bewail
C
9

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.

Cannes answered 31/10, 2017 at 18:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.