C语言 为什么我收到“数组初始值设定项必须是初始值设定项列表或字符串文字”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44356121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:40:18  来源:igfitidea点击:

Why am I getting "array initializer must be an initializer list or string literal"?

carrays

提问by

I am completely new to C programming and the language I used to work on before C was Java. I am trying to get a method to return a char array and it is not working. Here is my syntax:

我对 C 编程和我在 C 成为 Java 之前曾经使用过的语言完全陌生。我正在尝试获取一种返回字符数组的方法,但它不起作用。这是我的语法:

char * insertToArray(char * val){
    int k;
    char arr[2] = val;
    // do some other staffs here to the value
    return arr;
}

int main(){
    char s1[] = {"one", "two"};
    char newArr[];
    int i;

    for(i = 0; i < 2; i++){
        newArr[] = insertToArray(s1[i]);
    } 

    return 0;
}

All I am trying to do is pass the s1array to insertToArrayand do some kind of calculation on the values, then return a whole new single array. I am a complete beginner and I couldn't find any other help online. What am I doing wrong?

我想要做的就是将s1数组传递给insertToArray并对值进行某种计算,然后返回一个全新的单个数组。我是一个完整的初学者,我在网上找不到任何其他帮助。我究竟做错了什么?

采纳答案by Some programmer dude

The rules of C says that you can't initialize an array using a pointer. Instead define the array then copy to it:

C 的规则说不能使用指针初始化数组。而是定义数组然后复制到它:

char arr[strlen(val) + 1];  // Make sure there's enough space
strcpy(arr, val);

Then you can not define empty arrays. An array must have a size. And using the array newArrin the mainfunction is wrong anyway since the function you call returns a pointer. So newArrmust be a pointer as well.

那么你不能定义空数组。数组必须有大小。无论如何newArr,在main函数中使用数组是错误的,因为您调用的函数返回一个指针。所以也newArr必须是一个指针。



Now with that out of the way, there are a couple of other things in your (current) code that are very wrong.

现在,除此之外,您的(当前)代码中还有其他一些非常错误的地方。

The first being the size of the array arr. An array of two characters can only hold space for one-character string. Remember that strings are null terminated, there must be space for the full string plusthe terminator.

第一个是数组的大小arr。两个字符的数组只能容纳一个字符的字符串的空间。请记住,字符串是空终止的,必须有完整的字符串加上终止符的空间。

The second problem is that you return a pointer to a local variable. Once the function insertToArrayreturns, all its local variables cease to exist. Having a pointer to one of those variables will lead to undefined behaviorwhen you use it.

第二个问题是你返回一个指向局部变量的指针。一旦函数insertToArray返回,它的所有局部变量就不再存在。使用指向这些变量之一的指针将导致未定义的行为

The fix to the first problem is shown above. The fix to the second problem is a little harder, and involves either passing an extra argument to the function or allocating memory dynamically. I recommend the extra argument way:

上面显示了对第一个问题的修复。解决第二个问题有点困难,它涉及向函数传递额外的参数或动态分配内存。我推荐额外的论证方式:

char * insertToArray(const char * val, char * arr){
    strcpy(val, arr);

    // do some other staffs here to the value

    return arr;
}

Then call it like

然后称之为

char newArr[strlen(s1[i]) + 1];
insertToArray(s1[i], newArr);