Array storing pointer array problem


void setup() {
// put your setup code here, to run once:
char state1[8]= "GRRRGRRR";
char state2[8]= "RRGRRRGR";
char state3[8]= "GGRRRRRR";
char state4[8]= "RRGGRRRR";
char state5[8]= "RRRRGGRR";
char state6[8]= "RRRRRRGG";
char state7[8]= "RGRRRGRR";
char state8[8]= "RRRGRRRG";

char *previous[8] = {state1, state2, state3, state4, state5, state6, state7, state8 };



here, I have saved all array names in the pointer array. I'm just testing here. no code other than this. can anyone help me to access and print the variable name here other than inside the variable. thank you

Nine into eight doesn't go.

1 Like

First, please use code tag.

That string is 9 bytes.
Doesn't fit in an 8 byte.

1 Like

C strings have a null byte as terminator, so that a string "a" takes 2 bytes, "aa" takes 3 bytes, etc. I think it would be simpler to declare the C strings as C strings anyway, like this:

char * state1 = "GRRRGRRR";
char * state2 = "RRGRRRGR";

Then you don't have the opportunity to get the char array length wrong...

1 Like

You only need to omit the number of chars in the definition:
char state1[]= "GRRRGRRR";
The compiler will count the characters and create a suitable array.

1 Like

sir what do you mean by 9bits.

oh.. is there a document for the coding sir. im a newbie.

ok sir i will. but my question is another one. i need to print the variable name through pointer array. only the name. is there a way sir

oh. I'm not much familiar with c strings sir.

I'm not sure what you really want to be printed to the serial monitor. The variable name cannot be printed. The uploaded sketch doesn't contain any variable name anymore.

1 Like

The variable name only exists in the code humans see and the compiled program doesn't remember about it.

1 Like

Actually that's probably better - especially as this is a state that's presumably going to change, rather than a static string.

1 Like

ok, sir. then I will use a simple if-else condition and do it. thank you very much. I just wanted to find if there is a possibility or not.

The words "bit" or "bits" don't appear in my answer, so I'm not sure what you're asking, but generally "one bit more than eight bits".

1 Like

String literals are read-only:

const char *state1 = "GRRRGRRR";
1 Like

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.