Ç复合文字,指针数组 [英] C compound literals, pointer to arrays

查看:101
本文介绍了Ç复合文字,指针数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图复合字面常量分配给一个变​​量,但它似乎不工作,请参阅:

I'm trying to assign a compound literal to a variable, but it seems not to work, see:

  int *p[] = (int *[]) {{1,2,3},{4,5,6}};

我在GCC一个错误。

I got a error in gcc.

但如果我只写这样的:

  int p[] = (int []) {1,2,3,4,5,6};

然后,它没关系。

Then it's okay.

不过,是不是我想要的。

But is not what I want.

我不明白,为什么出现错误;因为如果我初始化它像一个阵列,或字符数组的指针使用它,它的好,见:

I don't understand why the error occurrs, because if I initialize it like a array, or use it with a pointer of arrays of chars, its okay, see:

  int *p[] = (int *[]) {{1,2,3},{4,5,6}}; //I got a error
  int p[][3] = {{1,2,3},{4,5,6}}; //it's okay
  char *p[] = (char *[]) {"one", "two"...}; // it's okay!

请注意,我不明白为什么我得到了第一个错误,并请我不能,否则我不想写这样的第二种形式,因为它需要一个复合文字,和我不不想说有多大阵列编译器。我想是这样,第二个,但对于int值。

Note I don't understand why I got an error in the first one, and please I can't, or I don't want to write like the second form because it's needs to be a compound literals, and I don't want to say how big is the array to the compiler. I want something like the second one, but for int values.

先谢谢了。

推荐答案

首先,铸件在所有的例子多余的,可以删除。其次,您使用的初始化多维数组的语法,这需要以分配的内存连续块的定义的第二个维度。相反,请尝试以下两种方法之一:

First, the casts are redundant in all of your examples and can be removed. Secondly, you are using the syntax for initializing a multidimensional array, and that requires the second dimension the be defined in order to allocate a sequential block of memory. Instead, try one of the two approaches below:


  • 多维数组:

  • Multidimensional array:

int p[][3] = {{1,2,3},{4,5,6}};


  • 指针数组一维数组:

  • Array of pointers to one dimensional arrays:

    int p1[] = {1,2,3};
    int p2[] = {4,5,6};
    int *p[] = {p1,p2};
    


  • 后一种方法具有允许用于改变长度的子阵列的优点。然而,前一种方法可以确保内存连续布局。

    The latter method has the advantage of allowing for sub-arrays of varying length. Whereas, the former method ensures that the memory is laid out contiguously.

    这是我强烈建议您不要使用另一种方法是EN code在字符串中的整数。这是一个不可移植的黑客。另外,在字符串中的数据被认为是恒定的。做你的阵列需要是可变的?

    Another approach that I highly recommend that you do NOT use is to encode the integers in string literals. This is a non-portable hack. Also, the data in string literals is supposed to be constant. Do your arrays need to be mutable?

    int *p[] = (int *[]) {
        "\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00",
        "\x04\x00\x00\x00\x05\x00\x00\x00\x06\x00\x00\x00"
    };
    

    这例子可能是32位小端机器上工作,但我从一个iPad打字这一点,目前无法核实。再次,请不要使用;我甚至把它觉得脏。

    That example might work on a 32-bit little-endian machine, but I'm typing this from an iPad and cannot verify it at the moment. Again, please don't use that; I feel dirty for even bringing it up.

    在发现的铸造方法也出现的指针的指针运行。这可以被索引像多维数组也是如此。

    The casting method you discovered also appears to work with a pointer to a pointer. That can be indexed like a multidimensional array as well.

    int **p = (int *[]) { (int[]) {1,2,3}, (int[]) {4,5,6} };
    

    这篇关于Ç复合文字,指针数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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