0

I have this two arrays and I want to use them in each case for printing different array on my LCD.

Here is an example

char *ChangeSituatuion;
char *MainMenu[4]={"Category 1","Category 2","Category 3","Category 4"};
char *SubMenu[2]={"Category 3","Category 4"};

//case 1
*ChangeSituatuion=MainMenu;

//case 2
*ChangeSituatuion=SubMenu;


LCDPutStr(ChangeSituatuion[0],1);

With this example above i'm taking no meaning letters on my Lcd

Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123
Rhs
  • 15
  • 3
  • 1
    What's your question? – lurker Feb 04 '18 at 15:46
  • How to copy each array on my array ChangeSituatuion. – Rhs Feb 04 '18 at 15:46
  • Yes i need to copy first array in one situation. And in another situatuion second one – Rhs Feb 04 '18 at 15:48
  • I need the to create a code for changing my aray in lcd output like MainMenu+1; And then to go in next array. Should i use typedef union?? – Rhs Feb 04 '18 at 15:53
  • 1
    I don't know what you mean when you say you need to copy it. Why do you need to copy it? And where do you want to copy it to? Maybe you could elaborate on your menu example a bit more. – lurker Feb 04 '18 at 15:53
  • I have about ten situations. I dont want write the all code again and again Example LCDPutStr(MainMenu[0],1); LCDPutStr(SubMenu[0],1); LCDPutStr(SubMenu22[0],1); LCDPutStr(SubMenu23[0],1); .... and so on. I want to avoid this thing – Rhs Feb 04 '18 at 15:55

2 Answers2

0

If you want to copy the pointer, and not make a second array with the same content, I think this should work:

char **ChangeSituatuion;
char *MainMenu[4]={"Category 1","Category 2","Category 3","Category 4"};
char *SubMenu[2]={"Category 3","Category 4"};

//case 1
ChangeSituatuion=MainMenu;

//case 2
ChangeSituatuion=SubMenu;


LCDPutStr(ChangeSituatuion[0],1);

There, the array type is transformed into a pointer type. Take in mind that an array of length X has a different type than an array of length Y, but in any case, the casting to a pointer is trivial.

EDIT: Corrected typo.

0

You have an indirection level problem. You need a pointer to one array of pointers. Your code should be:

char **ChangeSituatuion;
char *MainMenu[4]={"Category 1","Category 2","Category 3","Category 4"};
char *SubMenu[2]={"Category 3","Category 4"};

//case 1
ChangeSituatuion=MainMenu;

//case 2
ChangeSituatuion=SubMenu;

LCDPutStr(ChangeSituatuion[0],1);
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • And one last thing is there any quick way to get size of ChangeSituatuion? Example int totalMainMenu = (sizeof(ChangeSituatuion)/sizeof(ChangeSituatuion[])); – Rhs Feb 04 '18 at 16:00
  • @Rhs: Never use sizeof to get the size of an array when what you have is just a pointer! It in the [FAQ](https://stackoverflow.com/q/492384/3545273)... – Serge Ballesta Feb 04 '18 at 16:55