Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C: make, pass and access const array of pointers to const strings

For some reason this isn't working:

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

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

Is this correct? (Is it code elsewhere that must be causing the problem?)

(I'm running it on a baseline 8bit microcontroller and have very limited debugging capabilities)

Clarification:

This is what I want to achieve:

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);
}

As well as corrections, any neater suggestions of doing the above would be welcome.

Various combinations of the command strings need to be sent to a function for processing. All the strings will be in ROM, as will the groups of pointers to them.

like image 495
CL22 Avatar asked Jan 22 '26 09:01

CL22


1 Answers

You created an array of pointers, but you are passing the addresses of pointers (meaning, a pointer to a pointer). what you should do is this-

    const char* commands[2]= {str_reset_command, str_config_command};
like image 83
Yaniv Nikan Avatar answered Jan 24 '26 00:01

Yaniv Nikan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!