分配适当的变量以使这些声明正确

Assign appropriate variables so that these declarations are correct

我得到了以下代码:

int TAB[4][6];
int (*FF(int k))[3];
char *NAP[]={"nap1", "nap2", "nap3"};
double T[][2]={{1.0,1.0},{0.},{2.0,3.0}};

我应该使这些声明正确(这样它们就不会 return 在编译器中出现任何错误):

a = &T;
b = FF;
c = FF(9);
d = TAB[2];
e = FF(9)[1];
f = *NAP+1;
g = *NAP[1]++;

我只做到了很少。我的编译器 (xCode) 没有 return 这些错误,但我不知道如何声明剩余的 3...

我设法做到的:

1.int (*c)[3]= FF(9); 
2.int *d = TAB[2];
3.char *f = *NAP+1;
4.char g = *NAP[1]++;
  1. FF函数return是一个指向三元素数组的指针,所以我们需要一个指向三元素数组的指针?
  2. 因为 TAB[2] 是第二个子数组,那么只需要一个指针就足够了?
  3. *NAP+1 指向数组中第一个单词的 "a" 字符的地址 NAP 所以我们需要一个指针来存储该地址?
  4. 这次 *NAP[1]++ 实际上是指向一个值,所以只需一个变量就可以完成这项工作?

在这里,我将向您展示如何像专业人士一样解决这些烦人的申报难题:作弊。使用 cdecl 程序,它也可以作为 service 找到。

例如:

cdecl> explain double T[][2];
declare T as array of array 2 of double

因此,声明 double T[][2];T 声明为 double 的数组 2 的数组。如果您使用 & 获取某物的 地址 ,该地址的类型是 指向某物的指针 。因此,由于 &T 的值已分配给 a,因此 a 应声明为指向数组 2 的 数组的 指针双:

cdecl> declare a as pointer to array of array 2 of double;
double (*a)[][2]

同样适用于 FF

cdecl> explain int (*FF(int))[3];
declare FF as function (int) returning pointer to array 3 of int

所以FF是一个函数。现在,要有一个可以分配的 变量 。一个函数的名字衰减到一个指向函数的指针——所以这里我们需要使用一个指针来函数:

cdecl> declare b as pointer to function (int) returning pointer to array 3 of int
int (*(*b)(int ))[3]

对于 c,因为 FF 是一个 函数 (int) 返回指向 int 数组 3 的指针,如果您调用 FFint (9) 作为参数,它 returns 指向 int:

数组 3 的 指针
cdecl> declare c as pointer to array 3 of int
int (*c)[3]

而对于 e,我们使用索引运算符取消引用指针,因此我们得到一个类型为 array of 3 int 的左值。一个 不能 将数组分配给变量,但数组左值会衰减为指向 first 元素的指针,因此 array of 3 int 衰减为 指向 int 的指针。好吧,你肯定知道如何声明一个指向 int 的指针,但我很懒,不想犯错,所以我只是将内容粘贴到 cdecl:

cdecl> declare e as pointer to int;
int *e;

最后得到:

int TAB[4][6];
int (*FF(int k))[3];
char *NAP[]={"nap1", "nap2", "nap3"};
double T[][2]={{1.0,1.0},{0.},{2.0,3.0}};

int main(void) {
    double (*a)[][2];
    int (*(*b)(int ))[3];
    int (*c)[3];
    int *d;
    int *e;
    char *f;
    char g;

    a = &T;
    b = FF;
    c = FF(9);
    d = TAB[2];
    e = FF(9)[1];
    f = *NAP+1;
    g = *NAP[1]++;
}