简体   繁体   English

C声明char *数组

[英]C Declaring array of char*

I thought you could declare an array, and then later initilze it. 我以为你可以声明一个数组,然后再启动它。

Like so 像这样

char* myArray[3];


//CODE INBETWEEN 

myArray[3] = {

            "blah",
            "blah",
            "blah"};

Nope, you can only initialize an array when you first declare it. 不,你只能在第一次声明时初始化一个数组。 The reason is that arrays are not modifiable lvalues. 原因是数组不是可修改的左值。

In your case: 在你的情况下:

char *array[] = {"blah", "blah", "blah"};

You don't need to specify the size, but you can if you want. 您无需指定大小,但您可以根据需要指定大小。 However, the size can't be less than 3 in this case. 但是,在这种情况下,尺寸不能小于3。 Also, the three strings are written to read-only memory so something like array[1][2] = 'c' to change the 2nd "blah" to "blch" will normally result in a segfault. 此外,这三个字符串被写入只读存储器,因此像array[1][2] = 'c'这样将第二个“blah”更改为“blch”通常会导致段错误。

As others have said you can only use initialisers when the variable is declared. 正如其他人所说,只有在声明变量时才能使用初始化器。 The closest way to do what you want is: 最接近你想要的方法是:

char *myArray[3];

/* CODE INBETWEEN */

{
    static const char *tmp[3] = {
            "blah",
            "blah",
            "blah" };
    memcpy(myArray, tmp, sizeof myArray);
}

Yes, you can declare an array and then later initialize it. 是的,您可以声明一个数组,然后再初始化它。
However, there is an exception here. 但是,这里有一个例外。
You are declaring a character pointer array (which worked fine). 您正在声明一个字符指针数组 (工作正常)。
And, then you are instantiating constant strings to assign them to the array . 然后,您将实例化常量字符串以将它们分配给数组

That is where the problem starts. 这就是问题开始的地方。
Constant strings are just compiler primitives that do not get any addressable memory location in the way you have used them. 常量字符串只是编译器原语,它们不会像您使用它们那样获得任何可寻址的内存位置。 They can be assigned right at the array initialization time (as Mike has shown); 它们可以在阵列初始化时分配(如Mike所示); that will instruct the compiler to allocate them as constants available at run-time and allow an initialization when the scope of myArray starts. 这将指示编译器将它们分配为在运行时可用的常量,并允许在myArray的范围启动时进行初始化。


What you have tried would have worked well with 你尝试过的东西会很好用

int numberArray[3];
// code here
numberArray[0] = 1;
numberArray[1] = 2;
numberArray[2] = 3;

It helps to note that a character pointer and a string instance are two different entities; 有必要注意,字符指针和字符串实例是两个不同的实体; the first can point to the second. 第一个可以指向第二个。

You thought wrong. 你错了。 Initialization is only possible at declaration. 初始化只能声明时进行。 After that, you can only assign individual values. 之后,您只能分配单个值。

It's an initializer expression. 这是一个初始化表达式。 Can't have that code in between, got to be used on the line withe declaration. 中间不能有那个代码,必须在声明的线上使用。

It's not exactly clear which real world problem you're trying to solve, here... 目前还不清楚你要解决的是哪个现实问题 ,这里......

I thought you could declare an array, 我以为你可以声明一个数组,

... that is possible in so many ways, but for the sake of this question I want to focus on declaring an array inside of a struct . ...这是在很多方面可能的,但对于这个问题的缘故,我要专注于声明的内部数组struct

struct my_struct {
    char *my_array[3];
};

and then later initilze it. 然后再启动它。

Like so 像这样

struct my_struct fubar;
fubar = (struct my_array) { { "hello", "world" } };

Technically speaking this is initialisation of a different array (within an anonymous object) followed by an assignment of the entire struct . 从技术上讲,这是一个不同数组的初始化(在一个匿名对象中),然后是整个struct的赋值。 It might be useful to solve your real world problem (whatever that is)... but hopefully it will ultimately be useful for someone else later on to know that you can assign struct s with arrays inside of them... 解决你的现实世界问题 (无论是什么) 可能是有用的...但希望最后知道你可以在其中分配带有数组的struct的最终用途...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM