[英]C multidimentional char array - assignment makes integer from pointer without a cast
I create a big 2d char array and want to assign strings to it.我创建了一个大的 2d char 数组并想为其分配字符串。
int i;
char **word;
int start_size = 35000;
word=(char **) malloc(start_size*sizeof(char *));
for(i=0;i<start_size;i++)
word[i]=(char *) malloc(start_size*sizeof(char));
word[2][2] = "word";
how do I assign a string?我如何分配一个字符串? Explain me why this code doesn't work... I am new to low level programming and C but experienced in high level programming向我解释为什么这段代码不起作用......我是低级编程和 C 的新手,但在高级编程方面有经验
You cannot do string assignment in C.你不能在 C 中进行字符串赋值。
You need to call a function, sepcifically strcpy()
(prototype in <string.h>
)您需要调用一个函数,特别是strcpy()
( <string.h>
原型)
#include <string.h>
strcpy(word[2], "word");
You have to decide if you want a list of strings or a 2D array of strings.您必须决定是需要字符串列表还是二维字符串数组。
A list of string works like this:字符串列表的工作方式如下:
char **word;
word = (char**)malloc(start_size*sizeof(char*));
word[2] = "word";
In this example word[2]
would be the third string in the list and word[2][1]
would be the second character in the third string.在此示例中, word[2]
将是列表中的第三个字符串,而word[2][1]
将是第三个字符串中的第二个字符。
If you want a 2D array of string you have to do this:如果你想要一个二维字符串数组,你必须这样做:
int i;
char ***word;
^^^ 3 stars
int start_size = 35000;
word = (char***)malloc(start_size*sizeof(char**));
^^^ 3 stars ^^^ 2 stars
for(i=0;i<start_size;i++)
word[i] = (char**) malloc(start_size*sizeof(char*));
^^^ 2 stars ^^^ 1 star
word[2][2] = "word"; // no it works!
Note that in C you do not need the casts before the malloc
.请注意,在 C 中,您不需要malloc
之前的强制转换。 So this would also work:所以这也可以:
word = malloc(start_size*sizeof(char**));
word[2][2] = "word";
In the above statement, the string literal "word"
is implicitly converted to a pointer to its first element which has type char *
whereas word[2][2]
has type char
.在上面的语句中,字符串文字"word"
被隐式转换为指向其第一个元素的指针,该元素的类型为char *
而word[2][2]
类型为char
。 This attempts to assign a pointer to a character.这试图分配一个指向字符的指针。 This explains the warning message you have stated -这解释了您所说的警告消息 -
assignment makes integer from pointer without a cast
You can use string literals only to initialize character arrays.您只能使用字符串文字来初始化字符数组。 What you need to do is use the standard function strcpy
to copy the string literal.您需要做的是使用标准函数strcpy
来复制字符串文字。 Also, you should not cast the result of malloc
.此外,您不应malloc
的结果。 Please read this - Do I cast the result of malloc?请阅读这个 -我是否转换了 malloc 的结果? I suggest the following changes -我建议进行以下更改-
int i;
int start_size = 35000;
// do not cast the result of malloc
char **word = malloc(start_size * sizeof *word);
// check word for NULL in case malloc fails
// to allocate memory
for(i = 0; i < start_size; i++) {
// do not cast the result of malloc. Also, the
// the sizeof(char) is always 1, so you don't need
// to specify it, just the number of characters
word[i] = malloc(start_size);
// check word[i] for NULL in case malloc
// malloc fails to allocate memory
}
// copy the string literal "word" to the
// buffer pointed to by word[2]
strcpy(word[2], "word");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.