Hello im confused with pointers, how do i create a shallow copy of a string array in c ?

char *currMenuArray;  // ???

char *MainMenuArray[] =
    {
    "MAIN MENU",
    "One",
    "Two",
    "Options",
    "Quit"
    };


char *OptionsMenuArray[] =
    {
    "OPTION MENU",
    "Option1",
    "Option2",
    "Option3",
    "Back"
    };

void main()
{

  currMenuArray = MainMenuArray;  // ???

  DisplayMenu();

}

Recommended Answers

All 4 Replies

Your strings have no deep portions to them, so I'm confused. A deep part would be an array of pointers, where each pointer might point to a string, someplace else in memory.

You see deep structures commonly. The struct has a char * as one of the struct members.

Did you mean something like this?

/* pointer copy of a string */

#include <stdio.h>

int main() {
  char s1[]="What a great day to get out of a collapsed mine!";
  char s2[]="                                                ";
  char *sOne = s1;
  char *sTwo = s2;
  printf("\n\n");

  while(*sOne) {
    *sTwo++ = *sOne++;
  }
  printf("S1: %s   \nS2: %s", s1, s2);

  printf("\n\n\t\t\t     press enter when ready");
  (void) getchar();
  return 0;
}

nah i was just trying to find out how to point to an array of strings so i can just have a currentMenuArray which can either point to the MainMenuArray or the OptionsMenuArray instead of having two seperate Display() functions for each one

i just had to do this

char **gCurrMenuArray;

and it works now :)

pointer to a pointer is it ? do u know if its a good idea to code like that or not ?

It's standard, if not for beginners.

Three ampersand (star) programmers are generally mocked a bit, however.

>It's standard, if not for beginners.
Which is the single most annoying problem with how C is taught. Pointers are an integral part of the language, and really very simple conceptually. It's my opinion that they should be introduced as early as possible.

>Three ampersand (star) programmers are generally mocked a bit, however.
Three is uncommon, but fine. Four or more might raise some eyebrows though. I've never used more than five in production code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.