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

时间:2021-10-08 21:01:35

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之前使用的语言。我试图得到一个方法来返回一个char数组,它不起作用。这是我的语法:

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 s1 array to insertToArray and 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并对值进行某种计算,然后返回一个全新的单个数组。我是一个完整的初学者,我在网上找不到任何其他帮助。我究竟做错了什么?

1 个解决方案

#1


2  

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 newArr in the main function is wrong anyway since the function you call returns a pointer. So newArr must be a pointer as well.

然后你无法定义空数组。数组必须具有大小。并且在main函数中使用数组newArr无论如何都是错误的,因为你调用的函数返回一个指针。所以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 plus the terminator.

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

The second problem is that you return a pointer to a local variable. Once the function insertToArray returns, all its local variables cease to exist. Having a pointer to one of those variables will lead to undefined behavior when 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);

#1


2  

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 newArr in the main function is wrong anyway since the function you call returns a pointer. So newArr must be a pointer as well.

然后你无法定义空数组。数组必须具有大小。并且在main函数中使用数组newArr无论如何都是错误的,因为你调用的函数返回一个指针。所以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 plus the terminator.

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

The second problem is that you return a pointer to a local variable. Once the function insertToArray returns, all its local variables cease to exist. Having a pointer to one of those variables will lead to undefined behavior when 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);