简体   繁体   English

C 多维字符数组 - 赋值从指针生成整数而不进行强制转换

[英]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.

相关问题 当将char指针分配给字符数组时,赋值使整数指针不带强制转换[默认启用] - assignment makes pointer from integer without a cast [enabled by default] when assigning char pointer to character array C警告:赋值使指针从整数变成整数,而没有强制转换/指向整数数组的指针 - C warning: assignment makes integer from pointer without a cast / pointer to integer array 错误“赋值从没有强制转换数组的指针中获取整数= NULL” - Error “assignment makes integer from pointer without a cast array = NULL” 警告:赋值使指针从整数变成整数,而没有在数组中强制转换 - warning: assignment makes integer from pointer without a cast in array 警告赋值使指针来自整数而不在 C 中进行强制转换 - Warning assignment makes pointer from integer without a cast in C C程序-警告:赋值使指针从整数开始而没有强制转换 - C Program - warning: assignment makes pointer from integer without a cast “赋值在没有强制转换的情况下从指针生成整数” - “assignment makes integer from pointer without a cast” in c C Linux警告:赋值使指针不带强制转换而成为整数 - C Linux Warning: assignment makes integer from pointer without a cast C-赋值从指针进行整数转换而无需强制转换 - C - Assignment makes integer from pointer without a cast C-赋值使指针从整数开始而无需强制转换 - C - Assignment makes integer from pointer without cast
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM