Double Pointers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Extremist
    New Member
    • Jan 2007
    • 94

    Double Pointers

    Hi there

    I program in Linux, C++.

    If you declare a char pointer in C++ , you would say
    Code:
    char *pointer;
    And if you want to allocate memory for that pointer you would say
    Code:
    pointer = new char[50];
    Now, if I want to declare a double pointer
    Code:
    char **pointer;
    How would you allocate memory for such a double pointer?
  • Silent1Mezzo
    New Member
    • Feb 2007
    • 208

    #2
    Originally posted by Extremist
    Hi there

    I program in Linux, C++.

    If you declare a char pointer in C++ , you would say
    Code:
    char *pointer;
    And if you want to allocate memory for that pointer you would say
    Code:
    pointer = new char[50];
    Now, if I want to declare a double pointer
    Code:
    char **pointer;
    How would you allocate memory for such a double pointer?
    [CODE=cpp]char **pointer;
    pointer= new *char[50];[/CODE]
    That's how you can declare a double pointer

    PS...I'm a c programmer...no t having to malloc it is really nice.

    Comment

    • Extremist
      New Member
      • Jan 2007
      • 94

      #3
      Originally posted by Silent1Mezzo
      [CODE=cpp]char **pointer;
      pointer= new *char[50];[/CODE]
      That's how you can declare a double pointer

      PS...I'm a c programmer...no t having to malloc it is really nice.
      Ok...
      I did that

      I get a segmentation fault

      I have established that if only saying
      Code:
      char *pointer[50];
      works nicely, but I was just wondering
      Thanx for your input though.

      PS I'm also used to C myself

      Comment

      • ilikepython
        Recognized Expert Contributor
        • Feb 2007
        • 844

        #4
        Originally posted by Extremist
        Ok...
        I did that

        I get a segmentation fault

        I have established that if only saying
        Code:
        char *pointer[50];
        works nicely, but I was just wondering
        Thanx for your input though.

        PS I'm also used to C myself
        I think double pointers are declared like this:
        [code=cpp]
        char **pointer;
        pointer = new char*[50];
        [/code]
        I tried it and it works.
        Last edited by ilikepython; Jun 12 '07, 08:55 PM. Reason: rephrased

        Comment

        • emaghero
          New Member
          • Oct 2006
          • 85

          #5
          Originally posted by Extremist
          Hi there

          I program in Linux, C++.

          If you declare a char pointer in C++ , you would say
          Code:
          char *pointer;
          And if you want to allocate memory for that pointer you would say
          Code:
          pointer = new char[50];
          Now, if I want to declare a double pointer
          Code:
          char **pointer;
          How would you allocate memory for such a double pointer?
          If by "double pointer" you mean a pointer to a pointer use the following:

          Code:
          char **p=new(char *[size]);//size MUST be of type int
          //round brackets are for clarity
          //the code will work without them
          This will work for pointers of any type (Naturally you can't mix types).

          Dynamic memory allocation is second nature to me.
          All hail the new operator.

          Comment

          • Extremist
            New Member
            • Jan 2007
            • 94

            #6
            Oh, thank you soooo much!!!!
            You really helped me!!! I'm really greatful!
            I've been struggling somehow with this, so thank you

            Zel ;)

            Comment

            • Arvind07
              New Member
              • Jul 2007
              • 1

              #7
              Kindly check this program for double pointer allocation
              [code=c]
              #include <stdio.h>

              int main ()
              {
              int **p; /* 3*4 allocation */
              int i,j;
              p = malloc (3*sizeof (int));
              printf ("Address of p:%p\n", p);

              for (i=0; i<4; i++)
              {
              *(p+i) = malloc (4*sizeof (int));
              printf ("Address (%p)\n", *(p+i));

              }
              printf ("Address of *p:%p\n", *p);
              **p = 12;
              *(*p+1) = 14;
              *(*p+2) = 16;
              *(*p+3) = 18;

              **(p+1) = 5;
              *(*(p+1)+1) = 6;
              *(*(p+1)+2) = 7;
              *(*(p+1)+3) = 8;

              **(p+2) = 9;
              *(*(p+2)+1) = 10;
              *(*(p+2)+2) = 11;
              *(*(p+2)+3) = 12;



              for (i=0; i<3; i++)
              {
              for (j=0; j<4; j++)
              {
              printf (" Array (%d) \n", *(*(p+i)+j));
              }
              printf ("One row finished \n");
              }
              } // main ()
              [/code]
              Last edited by weaknessforcats; Jul 6 '07, 03:52 PM. Reason: added code tags

              Comment

              • Silent1Mezzo
                New Member
                • Feb 2007
                • 208

                #8
                Originally posted by Arvind07
                Kindly check this program for double pointer allocation
                #include <stdio.h>

                int main ()
                {
                int **p; /* 3*4 allocation */
                int i,j;
                p = malloc (3*sizeof (int));
                printf ("Address of p:%p\n", p);

                for (i=0; i<4; i++)
                {
                *(p+i) = malloc (4*sizeof (int));
                printf ("Address (%p)\n", *(p+i));

                }
                printf ("Address of *p:%p\n", *p);
                **p = 12;
                *(*p+1) = 14;
                *(*p+2) = 16;
                *(*p+3) = 18;

                **(p+1) = 5;
                *(*(p+1)+1) = 6;
                *(*(p+1)+2) = 7;
                *(*(p+1)+3) = 8;

                **(p+2) = 9;
                *(*(p+2)+1) = 10;
                *(*(p+2)+2) = 11;
                *(*(p+2)+3) = 12;



                for (i=0; i<3; i++)
                {
                for (j=0; j<4; j++)
                {
                printf (" Array (%d) \n", *(*(p+i)+j));
                }
                printf ("One row finished \n");
                }
                } // main ()
                You're kind of hijacking this post. You should start your own probably.

                Comment

                • j3n0vacHiLd
                  New Member
                  • Sep 2007
                  • 1

                  #9
                  2D array using a pointer to a pointer.
                  Code:
                  char **p;
                  p = new char*[50]; // p = new *char[50]; will give you a syntax error
                  *p = new char[50];
                  p[0][0] = 'a';
                  p[0][1] = 'b';
                  p[0][2] = 'b';
                  Just don't forget if you don't want any memory leaks.

                  Code:
                  delete [] *p;
                  delete [] p;
                  ----

                  Comment

                  • evillestat
                    New Member
                    • Mar 2008
                    • 4

                    #10
                    Originally posted by j3n0vacHiLd
                    2D array using a pointer to a pointer.
                    Code:
                    char **p;
                    p = new char*[50]; // p = new *char[50]; will give you a syntax error
                    *p = new char[50];
                    p[0][0] = 'a';
                    p[0][1] = 'b';
                    p[0][2] = 'b';
                    Just don't forget if you don't want any memory leaks.

                    Code:
                    delete [] *p;
                    delete [] p;
                    ----

                    Not to dig up terribly old code, but I saw this and was curious.

                    I have not tried this, but does this work?


                    p = new char*[50]; // p = new *char[50]; will give you a syntax error
                    *p = new char[50];


                    As in, this would be the same as:

                    char p[50][50];

                    I was always told you had to use a for loop to allocate the memory dynamically for the double pointer 2-d array.

                    Thusly:

                    p = new int *[r];
                    for(i = 0; i < r; i++)
                    {
                    p[r] = new int[c];
                    }

                    Where R and C are rows and columns, respectively.

                    Any thoughts?

                    Comment

                    • Ganon11
                      Recognized Expert Specialist
                      • Oct 2006
                      • 3651

                      #11
                      [CODE=cpp]p = new char*[50]; // p = new *char[50]; will give you a syntax error
                      *p = new char[50];
                      [/CODE]

                      means that p is a pointer to an array of 50 pointers to characters. The first pointer in this array, p[0] (or *p), points to an array of 50 characters. None of the other pointers in p point to anything.

                      Comment

                      • evillestat
                        New Member
                        • Mar 2008
                        • 4

                        #12
                        Originally posted by Ganon11
                        [CODE=cpp]p = new char*[50]; // p = new *char[50]; will give you a syntax error
                        *p = new char[50];
                        [/CODE]

                        means that p is a pointer to an array of 50 pointers to characters. The first pointer in this array, p[0] (or *p), points to an array of 50 characters. None of the other pointers in p point to anything.

                        So I was correct in thinking this would not work? Becuase if p[0] is pointing to an array of 50 characters, and nothing else is point anywhere, then it isn't a 2-d array of p[50][50] but p[0] = p[50]

                        So then you would have to set it another way, correct?

                        Comment

                        • Ganon11
                          Recognized Expert Specialist
                          • Oct 2006
                          • 3651

                          #13
                          Exactly .

                          Comment

                          Working...