指向整数数组的指针的标量初始值设定项的多余元素 [英] Excess elements of scalar initializer for pointer to array of ints

查看:71
本文介绍了指向整数数组的指针的标量初始值设定项的多余元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 K&R 中进行练习(例如 5-9)并且我试图转换原始程序的二维数组

I’m working on an exercise in K&R (ex. 5–9) and I was trying to convert the original program’s 2D array of

static char daytab[2][13] = {
    {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
    {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};

使用指向 13 个整数数组的指针,例如

into using pointers to an array of 13 ints like

static char (*daytab)[13] = {
    {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
    {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};

但编译器打印警告:标量初始化器中的元素过多.

But compiler prints warning: excess elements in scalar initializer.

在将数组传递给函数时,谷歌搜索没有帮助,甚至 K&R 写入,

Googling did not help and even K&R writes when passing the array to a function,

myFunction(int daytab[2][13]) {...}

myFunction(int (*daytab)[13]) {...}

推荐答案

两者只是部分等效.区别在于:

The two are only partly equivalent. The difference being that:

static char daytab[2][13] = {
    {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, 
    {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};

声明一个二维数组,其中包括为数组留出空间并确保 daytab 引用该内存.然而:

declares a two-dimensional array, which includes setting aside space for the array and ensuring that daytab references that memory. However:

static char (*daytab)[13] = {
    {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, 
    {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};

...只声明一个指针.因此,您尝试使用数组初始值设定项初始化一个指针,但它没有按预期工作.没有数组;没有为数组留出内存.相反,您的初始值设定项中的第一个数字被分配给指针 daytab,并且编译器生成一个警告,让您知道您已经指定了许多刚刚被丢弃的附加值.由于初始值设定项中的第一个数字是 0,因此您只是将 daytab 设置为 NULL 以一种相当冗长的方式.

...only declares a pointer. So you're trying to initialize a pointer with an array initializer, which doesn't work as expected. There is no array; there's no memory set aside for an array. What happens instead is that the first number in your initializer is assigned to the pointer daytab, and the compiler generates a warning to let you know you've specified a lot of additional values that are just discarded. Since the first number in your initializer is 0, you're just setting daytab to NULL in a rather verbose way.

因此,如果您想进行此类初始化,请使用第一个版本——它会衰减为您在第二个版本中明确声明的相同指针类型,因此您可以以相同的方式使用它.当您希望动态分配数组或获取对已存在的另一个数组的引用时,需要带有数组指针的第二个版本.

So if you want to do this sort of initialization, use the first version -- it decays to the same pointer type that you explicitly declare in the second version, so you can use it the same way. The second version, with the array pointer, is needed when you wish to dynamically allocate the array or get a reference to another array that already exists.

所以你可以这样做:

static char arr[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
static char (*ptr)[3] = NULL;

ptr = arr;

...然后交替使用 ptrarr .或者这个:

...and then use ptr and arr interchangeably. Or this:

static char (*ptr)[3] = NULL;

ptr = malloc(2 * sizeof(*ptr));

...得到一个动态分配的二维数组(不是指向一维数组的指针数组,而是一个真正的二维数组).当然,在这种情况下它不会被初始化.

...to get a dynamically allocated 2-dimensional array (not an array of pointers to 1D arrays, but a real 2D array). Of course, it's not initialized in that case.

这两种变体的等价"仅意味着二维数组,当它衰减到指向其第一个元素的指针时,衰减到第二个变体中声明的指针类型.一旦指针版本实际指向一个数组,两者是等价的.但是二维数组版本为数组设置了内存,其中指针声明不会......并且可以为指针分配一个新值(指向不同的数组),而二维数组变量则不能.

The "equivalence" of the two variations just means that the 2D array, when it decays to a pointer to its first element, decays to the type of pointer declared in the second variation. Once the pointer version is actually pointed at an array, the two are equivalent. But the 2D array version sets up memory for the array, where the pointer declaration doesn't... and the pointer can be assigned a new value (pointed at a different array) where the 2D array variable cannot.

在 C99 中你可以这样做(如果不是 static 至少):

In C99 you can do this, though (if not static at least):

char (*daytab)[13] = (char [][13]){
    {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, 
    {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};

这篇关于指向整数数组的指针的标量初始值设定项的多余元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆