gpt4 book ai didi

c - 初始化后无法将字符串放入数组中 - 使用适用于 ARM 的 TI 编译器 5.1.8 的 CCS 6

转载 作者:行者123 更新时间:2023-11-30 17:25:23 24 4
gpt4 key购买 nike

我正在尝试为 AM335x 处理器上运行的应用程序设置多语言 GUI;在 CCS 6.0.1 中开发并使用 TI 编译器 5.1.8。这个概念是获取枚举字典数组,然后将当前字典指针切换到其中之一,以便我能够使用有意义的枚举。

enum dictionary_indexes {
name,
surname,
phone
}

const char *dictionary_en[60];

dictionary_en[name] = "Your name";
dictionary_en[surname] = "Your surname";
//and so on

不幸的是,CCS 不会编译这样的代码。它只允许在声明时初始化数组:

//compiles nicely:
const char * const teststring[] = {
"String1",
"String2",
};

//doesn't compile:
const char *teststring2[2];
teststring2[0]="Hello";
teststring2[1]="World";

这样的代码会导致错误

“char [6]”类型的值不能用于初始化“int [0]”类型的实体

对于每个数组条目也是如此。

我在这里遗漏了什么吗?我过去使用过这样的代码并且工作得很好。这是 TI 的编译器问题,还是处理器特有的问题?应该工作的代码基于此线程:How do I create an array of strings in C?

最佳答案

teststring2 必须是全局变量,但它的 init 不能。正如 @barakmanos 所建议的,将 init 封装在可执行函数中的一个小 rafactor 可以带来缓解和正确的编译。

const char *teststring2[2];

void initDict(){

teststring2[0]="Hello";
teststring2[1]="World";
}

关于c - 初始化后无法将字符串放入数组中 - 使用适用于 ARM 的 TI 编译器 5.1.8 的 CCS 6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27078767/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com