C指针中的"p = (int[2]){*p};"行是什么意思?
What is the meaning of "p = (int[2]){*p};" line in C pointer?
据我了解,我正在将变量 a
的地址传递给函数 int ffx1
。
之后,p = (int[2]){*p};
这一行到底是什么意思?
int ffx1(int * p)
{
p = (int[2]){*p};
return(p[1]);
}
int main()
{
int a = 1;
a = ffx1(&a);
printf("%d", a);
return 0;
}
对于 int ffx1(int * p)
,p
是指向 int
的指针。
(int[2]){*p}
是一个 复合文字 定义一个 int
数组 2。 这个未命名的数组我们将调用 X[2]
.它的第一个元素的值为 *p
,而作为 int
的下一个元素将被初始化为零(§6.7.9 21 & 10),因为它没有明确列出。
p = ....
将指针 p
分配给了一个新值。在这种情况下,上面的数组 X[]
被转换为其第一次注册的地址或 &X[0]
.
return p[1]
取消引用 p
,返回 X[1]
或 0
.
中的值
它是一个指向复合文字的指针。
C11-§6.5.2.5 复合文字 (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.
据我了解,我正在将变量 a
的地址传递给函数 int ffx1
。
之后,p = (int[2]){*p};
这一行到底是什么意思?
int ffx1(int * p)
{
p = (int[2]){*p};
return(p[1]);
}
int main()
{
int a = 1;
a = ffx1(&a);
printf("%d", a);
return 0;
}
对于 int ffx1(int * p)
,p
是指向 int
的指针。
(int[2]){*p}
是一个 复合文字 定义一个 int
数组 2。 X[2]
.它的第一个元素的值为 *p
,而作为 int
的下一个元素将被初始化为零(§6.7.9 21 & 10),因为它没有明确列出。
p = ....
将指针 p
分配给了一个新值。在这种情况下,上面的数组 X[]
被转换为其第一次注册的地址或 &X[0]
.
return p[1]
取消引用 p
,返回 X[1]
或 0
.
它是一个指向复合文字的指针。
C11-§6.5.2.5 复合文字 (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.