我得到了“无效初始值设定项”,我做错了什么?

时间:2021-06-11 22:44:56
int main(void) {
    char testStr[50] = "Hello, world!";
    char revS[50] = testStr;
}

I get error: "invalid initializer" on the line with revS. What am I doing wrong?

我在与rev的行上出现了错误:“无效的初始值设定项”。我做错了什么?

6 个解决方案

#1


21  

Because you can't initialise like that, you need a constant expression as the initialisation value. Replace it with:

因为不能像这样初始化,所以需要一个常量表达式作为初始化值。换成:

int main (void) {
    char testStr[50] = "Hello, world!";
    char revS[50]; strcpy (revS, testStr);
    :
}

Or, if you really want initialisation, you can use something like:

或者,如果你真的想要初始化,你可以使用如下的东西:

#define HWSTR "Hello, world!"
int main (void) {
    char testStr[50] = HWSTR;
    char revS[50] = HWSTR;
    :
}

This provides a constant expression with minimal duplication in your source.

这提供了一个在您的源中最小重复的常量表达式。

#2


7  

Arrays arent assignable.

数组不分配。

You should use memcpy to copy contents from testStr to revS

您应该使用memcpy从testStr复制内容到rev。

memcpy(revS,testStr,50);

#3


4  

Only constant expressions can be used to initialize arrays, as in your initialization of testStr.

只有常量表达式可以用于初始化数组,如在testStr的初始化中。

You're trying to initialize revS with another array variable, which is not a constant expression. If you want to copy the contents of the first string into the second, you'll need to use strcpy.

你尝试用另一个数组变量来初始化rev,这不是一个常量表达式。如果要将第一个字符串的内容复制到第二个字符串,则需要使用strcpy。

#4


2  

An initializer for a char[] needs to be either a literal string or something like {1,2,3,4}. It isn't allowed to be the name of another variable.

char[]的初始化器需要是一个字符串,或者类似{1,2,3,4}。它不允许是另一个变量的名称。

#5


1  

You are doing

你正在做的事情

char revS[50] = testStr; 

which is wrong since you cannot assign char * to char.

这是错误的,因为您不能将char *分配给char。

Try revS = testStr; it should work.

试着转速= testStr;它应该工作。

#6


0  

Unless you plan on manipulating the second array you can also use a pointer:

除非你打算操作第二个数组,否则你也可以使用一个指针:

int main(void){
    char textStr[50] = "hello worlds!";
    char *revS = textStr;
    printf("%s\n", revS);

}

}

If you want to get really crazy you can point to a specific location in the array with the reference operator:

如果你想变得非常疯狂,你可以指向数组中的一个特定位置与引用操作符:

int main(void){
    char textStr[50] = "hello worlds!";
    char *revS = &textStr[5];
    printf("%s\n", revS);
}

#1


21  

Because you can't initialise like that, you need a constant expression as the initialisation value. Replace it with:

因为不能像这样初始化,所以需要一个常量表达式作为初始化值。换成:

int main (void) {
    char testStr[50] = "Hello, world!";
    char revS[50]; strcpy (revS, testStr);
    :
}

Or, if you really want initialisation, you can use something like:

或者,如果你真的想要初始化,你可以使用如下的东西:

#define HWSTR "Hello, world!"
int main (void) {
    char testStr[50] = HWSTR;
    char revS[50] = HWSTR;
    :
}

This provides a constant expression with minimal duplication in your source.

这提供了一个在您的源中最小重复的常量表达式。

#2


7  

Arrays arent assignable.

数组不分配。

You should use memcpy to copy contents from testStr to revS

您应该使用memcpy从testStr复制内容到rev。

memcpy(revS,testStr,50);

#3


4  

Only constant expressions can be used to initialize arrays, as in your initialization of testStr.

只有常量表达式可以用于初始化数组,如在testStr的初始化中。

You're trying to initialize revS with another array variable, which is not a constant expression. If you want to copy the contents of the first string into the second, you'll need to use strcpy.

你尝试用另一个数组变量来初始化rev,这不是一个常量表达式。如果要将第一个字符串的内容复制到第二个字符串,则需要使用strcpy。

#4


2  

An initializer for a char[] needs to be either a literal string or something like {1,2,3,4}. It isn't allowed to be the name of another variable.

char[]的初始化器需要是一个字符串,或者类似{1,2,3,4}。它不允许是另一个变量的名称。

#5


1  

You are doing

你正在做的事情

char revS[50] = testStr; 

which is wrong since you cannot assign char * to char.

这是错误的,因为您不能将char *分配给char。

Try revS = testStr; it should work.

试着转速= testStr;它应该工作。

#6


0  

Unless you plan on manipulating the second array you can also use a pointer:

除非你打算操作第二个数组,否则你也可以使用一个指针:

int main(void){
    char textStr[50] = "hello worlds!";
    char *revS = textStr;
    printf("%s\n", revS);

}

}

If you want to get really crazy you can point to a specific location in the array with the reference operator:

如果你想变得非常疯狂,你可以指向数组中的一个特定位置与引用操作符:

int main(void){
    char textStr[50] = "hello worlds!";
    char *revS = &textStr[5];
    printf("%s\n", revS);
}