How can one return a 2D array from a function to the main?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 23354666
    New Member
    • Apr 2007
    • 3

    How can one return a 2D array from a function to the main?

    I have this simple program where I am trying to return a pointer to an array of chars, the output comes something like:
    0 : lllkkknnn
    1 : kkknnn
    2 : nnn

    where the first element in the array aa[][] takes all the variables of the other elements in the same array.

    I want the output to be something like:
    0 : lllkkknnn
    1 : kkknnn
    2 : nnn

    Appreciate your help.

    Code
    =============== =============== =======


    char* negotiateKeysMa ster();

    int main()
    {
    char* aa;
    int i;
    aa = negotiateKeysMa ster());

    printf("0 : %s \n",aa[0]);
    printf("1 : %s \n",aa[1]);
    printf("2 : %s \n",aa[2]);


    return 0;
    }

    char* negotiateKeysMa ster()
    {
    char keysChar[3][3];

    strcpy(keysChar[0],"lll");
    strcpy(keysChar[1],"kkk");
    strcpy(keysChar[2],"nnn");

    return &keysChar;
    }
  • 23354666
    New Member
    • Apr 2007
    • 3

    #2
    Sorry I want the output to be something like:
    0 : lll
    1 : kkk
    2 : nnn
    =============== =============== ============




    Originally posted by 23354666
    I have this simple program where I am trying to return a pointer to an array of chars, the output comes something like:
    0 : lllkkknnn
    1 : kkknnn
    2 : nnn

    where the first element in the array aa[][] takes all the variables of the other elements in the same array.

    I want the output to be something like:
    0 : lllkkknnn
    1 : kkknnn
    2 : nnn

    Appreciate your help.

    Code
    =============== =============== =======


    char* negotiateKeysMa ster();

    int main()
    {
    char* aa;
    int i;
    aa = negotiateKeysMa ster());

    printf("0 : %s \n",aa[0]);
    printf("1 : %s \n",aa[1]);
    printf("2 : %s \n",aa[2]);


    return 0;
    }

    char* negotiateKeysMa ster()
    {
    char keysChar[3][3];

    strcpy(keysChar[0],"lll");
    strcpy(keysChar[1],"kkk");
    strcpy(keysChar[2],"nnn");

    return &keysChar;
    }

    Comment

    • gpraghuram
      Recognized Expert Top Contributor
      • Mar 2007
      • 1275

      #3
      Hi,
      There are certain issues in your code
      1)First u cant retun address of a local variable
      2)u can get a char* to hold a char**

      Try this code
      Code:
      #include<stdio.h>
      #include<string.h>
      
      char** negotiateKeysMaster();
      
      int main()
      {
      char** aa=NULL;
      int i;
      aa = negotiateKeysMaster();
      printf("0 : %s \n",aa[0]);
      printf("1 : %s \n",aa[1]);
      printf("2 : %s \n",aa[2]);
      
      
      return 0;
      }
      
      char** negotiateKeysMaster()
      {
      	int lnVar=0;
      	char **keysChar=(char**)malloc(sizeof(char*)*3);
      	for(lnVar=0;lnVar<3;lnVar++)
      	{
      		keysChar[lnVar]=(char*)malloc(10);
      	}
      	strcpy(keysChar[0],"lll");
      	strcpy(keysChar[1],"kkk");
      	strcpy(keysChar[2],"nnn");
      	return keysChar; 
      }

      Comment

      • 23354666
        New Member
        • Apr 2007
        • 3

        #4
        It totally works, thank you.

        do you recommend anything to read about pointers? something handy and easy to understand?

        Regards,



        Originally posted by gpraghuram
        Hi,
        There are certain issues in your code
        1)First u cant retun address of a local variable
        2)u can get a char* to hold a char**

        Try this code
        Code:
        #include<stdio.h>
        #include<string.h>
        
        char** negotiateKeysMaster();
        
        int main()
        {
        char** aa=NULL;
        int i;
        aa = negotiateKeysMaster();
        printf("0 : %s \n",aa[0]);
        printf("1 : %s \n",aa[1]);
        printf("2 : %s \n",aa[2]);
        
        
        return 0;
        }
        
        char** negotiateKeysMaster()
        {
        	int lnVar=0;
        	char **keysChar=(char**)malloc(sizeof(char*)*3);
        	for(lnVar=0;lnVar<3;lnVar++)
        	{
        		keysChar[lnVar]=(char*)malloc(10);
        	}
        	strcpy(keysChar[0],"lll");
        	strcpy(keysChar[1],"kkk");
        	strcpy(keysChar[2],"nnn");
        	return keysChar; 
        }

        Comment

        • gpraghuram
          Recognized Expert Top Contributor
          • Mar 2007
          • 1275

          #5
          Hi,
          I read C by Kerningham and Ritchie.
          If u need u also can read it.

          Thanks
          Raghuram

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by gpraghuram
            Hi,
            I read C by Kerningham and Ritchie.
            If u need u also can read it.

            Thanks
            Raghuram
            K&R2 is the bible when it comes to C. When you've read it (and understood it)
            cover to cover you've done it all. I don't recommend the ANSI/ISO report for starters.

            kind regards,

            Jos

            Comment

            • horace1
              Recognized Expert Top Contributor
              • Nov 2006
              • 1510

              #7
              Originally posted by 23354666
              It totally works, thank you.

              do you recommend anything to read about pointers? something handy and easy to understand?

              Regards,
              as indicated by gpraghuram using malloc() is the obvious solution.
              You could return a pointer to a static local variable, e.g.
              Code:
              typedef char charArray[3][10];
              
              charArray* negotiateKeysMaster();
              
              int main()
              {
              charArray* aa;
              int i;
              aa = negotiateKeysMaster();
              printf("0 : %s \n",(*aa)[0]);
              printf("1 : %s \n",(*aa)[1]);
              printf("2 : %s \n",(*aa)[2]);
              
              system("pause");
              return 0;
              }
              
              charArray* negotiateKeysMaster()
              {
              static charArray keysChar;
              
              strcpy(keysChar[0],"lll");
              strcpy(keysChar[1],"kkk");
              strcpy(keysChar[2],"nnn");
              return &keysChar;
              }

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                However, using a static variable causes the array to continue to exist for the life of the program. You are better off creating the array on the heap and returning the address you get from the "new" operator:

                charArray* arr = new charArray;

                return arr;

                Then in main() you can:

                delete arr;

                when you are finished with it.

                The "static" keyword is a deprecated feature in C++.

                If you are writing in C, then use malloc() and free() insrtead of "new" and "delete".

                Comment

                Working...