我收到“无效的初始化程序",我做错了什么? [英] I'm getting "Invalid Initializer", what am I doing wrong?

查看:1285
本文介绍了我收到“无效的初始化程序",我做错了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我收到错误:revS行上出现无效的初始化程序".我在做什么错了?

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

推荐答案

由于不能像这样进行初始化,因此需要一个常量表达式作为初始化值.替换为:

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);
    // more code here
}

或者,如果您真的要初始化,则可以使用类似的方法:

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;
    // more code here
}

这将提供一个常量表达式,并且源代码中的重复项最少.

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

这篇关于我收到“无效的初始化程序",我做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆