gpt4 book ai didi

C: 创建、传递和访问指向常量字符串的常量指针数组

转载 作者:行者123 更新时间:2023-12-04 12:31:55 27 4
gpt4 key购买 nike

出于某种原因,这不起作用:

const char * str_reset_command = "\r\nReset";
const char * str_config_command = "\r\nConfig";

const char *commands[2] = {
&str_reset_command,
&str_config_command
};

这是正确的吗? (一定是其他地方的代码导致了问题吗?)

(我在基准 8 位微 Controller 上运行它并且调试能力非常有限)

澄清:

这是我想要实现的:

const char * str_reset_command = "\r\nReset";
const char * str_config_command = "\r\nConfig";
const char * str_start_command = "\r\nStart";
const char * str_end_command = "\r\nEnd";

const char * const group0[1] = {
str_reset_command
}
const char * const group1[2] = {
str_reset_command,
str_start_command
}
const char * const group2[3] = {
str_reset_command,
str_start_command,
str_end_command
}
void doStuffToCharacter(unsigned char the_char){
// ....
}
void doStuffToGroup(char ** group, unsigned char num_strings){
for (unsigned char s=0; s < num_strings; s++){
unsigned char idx = 0;
while (group[s][idx]){
doStuffToCharacter(group[s][idx]);
}
}
}
void main (void){
doStuff(group0, 1);
doStuff(group1, 2);
doStuff(group2, 3);
}

除了更正之外,我们欢迎任何关于执行上述操作的更简洁的建议。

命令字符串的各种组合需要发送到函数进行处理。所有字符串都在 ROM 中,指向它们的指针组也是如此。

最佳答案

您创建了一个指针数组,但您传递的是指针的地址(即指向指针的指针)。你应该做的是这个-

    const char* commands[2]= {str_reset_command, str_config_command};

关于C: 创建、传递和访问指向常量字符串的常量指针数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21429773/

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