By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,037 Members | 1,839 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,037 IT Pros & Developers. It's quick & easy.

Double Pointers

P: 94
Hi there

I program in Linux, C++.

If you declare a char pointer in C++ , you would say
Expand|Select|Wrap|Line Numbers
  1. char *pointer;
  2.  
And if you want to allocate memory for that pointer you would say
Expand|Select|Wrap|Line Numbers
  1. pointer = new char[50];
  2.  
Now, if I want to declare a double pointer
Expand|Select|Wrap|Line Numbers
  1. char **pointer;
  2.  
How would you allocate memory for such a double pointer?
Jun 12 '07 #1
Share this Question
Share on Google+
12 Replies


100+
P: 208
Hi there

I program in Linux, C++.

If you declare a char pointer in C++ , you would say
Expand|Select|Wrap|Line Numbers
  1. char *pointer;
  2.  
And if you want to allocate memory for that pointer you would say
Expand|Select|Wrap|Line Numbers
  1. pointer = new char[50];
  2.  
Now, if I want to declare a double pointer
Expand|Select|Wrap|Line Numbers
  1. char **pointer;
  2.  
How would you allocate memory for such a double pointer?
Expand|Select|Wrap|Line Numbers
  1. char **pointer;
  2. pointer= new *char[50];
That's how you can declare a double pointer

PS...I'm a c programmer...not having to malloc it is really nice.
Jun 12 '07 #2

P: 94
Expand|Select|Wrap|Line Numbers
  1. char **pointer;
  2. pointer= new *char[50];
That's how you can declare a double pointer

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

I get a segmentation fault

I have established that if only saying
Expand|Select|Wrap|Line Numbers
  1. char *pointer[50];
  2.  
works nicely, but I was just wondering
Thanx for your input though.

PS I'm also used to C myself
Jun 12 '07 #3

ilikepython
Expert 100+
P: 844
Ok...
I did that

I get a segmentation fault

I have established that if only saying
Expand|Select|Wrap|Line Numbers
  1. char *pointer[50];
  2.  
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:
Expand|Select|Wrap|Line Numbers
  1. char **pointer;
  2. pointer = new char*[50];
  3.  
I tried it and it works.
Jun 12 '07 #4

emaghero
P: 85
Hi there

I program in Linux, C++.

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

Expand|Select|Wrap|Line Numbers
  1. char **p=new(char *[size]);//size MUST be of type int
  2. //round brackets are for clarity
  3. //the code will work without them
  4.  
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.
Jun 12 '07 #5

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

Zel ;)
Jun 13 '07 #6

P: 1
Kindly check this program for double pointer allocation
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int main ()
  4. {
  5.    int **p; /* 3*4 allocation */
  6.    int i,j;
  7.        p = malloc (3*sizeof (int));
  8.       printf ("Address of p:%p\n", p);
  9.  
  10.       for (i=0; i<4; i++)
  11.       {
  12.         *(p+i) = malloc (4*sizeof (int)); 
  13.          printf ("Address (%p)\n", *(p+i));
  14.  
  15.       }
  16.       printf ("Address of *p:%p\n", *p);
  17.          **p = 12;
  18.          *(*p+1) = 14;
  19.          *(*p+2) = 16;
  20.          *(*p+3) = 18;
  21.  
  22.           **(p+1) = 5;
  23.          *(*(p+1)+1) = 6;
  24.          *(*(p+1)+2) = 7;
  25.          *(*(p+1)+3) = 8;
  26.  
  27.          **(p+2) = 9;
  28.          *(*(p+2)+1) = 10;
  29.          *(*(p+2)+2) = 11;
  30.          *(*(p+2)+3) = 12;
  31.  
  32.  
  33.  
  34.       for (i=0; i<3; i++)
  35.       { 
  36.          for (j=0; j<4; j++)
  37.          {
  38.             printf (" Array (%d) \n", *(*(p+i)+j));
  39.          }
  40.          printf ("One row finished \n");
  41.       }      
  42. } // main ()
  43.  
Jul 6 '07 #7

100+
P: 208
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.
Jul 6 '07 #8

P: 1
2D array using a pointer to a pointer.
Expand|Select|Wrap|Line Numbers
  1. char **p;
  2. p = new char*[50]; // p = new *char[50]; will give you a syntax error
  3. *p = new char[50];
  4. p[0][0] = 'a';
  5. p[0][1] = 'b';
  6. p[0][2] = 'b';
  7.  
Just don't forget if you don't want any memory leaks.

Expand|Select|Wrap|Line Numbers
  1. delete [] *p;
  2. delete [] p;
  3.  
----
Sep 11 '07 #9

P: 4
2D array using a pointer to a pointer.
Expand|Select|Wrap|Line Numbers
  1. char **p;
  2. p = new char*[50]; // p = new *char[50]; will give you a syntax error
  3. *p = new char[50];
  4. p[0][0] = 'a';
  5. p[0][1] = 'b';
  6. p[0][2] = 'b';
  7.  
Just don't forget if you don't want any memory leaks.

Expand|Select|Wrap|Line Numbers
  1. delete [] *p;
  2. delete [] p;
  3.  
----

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?
Apr 17 '08 #10

Ganon11
Expert 2.5K+
P: 3,652
Expand|Select|Wrap|Line Numbers
  1. p = new char*[50]; // p = new *char[50]; will give you a syntax error
  2. *p = new char[50];
  3.  
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.
Apr 17 '08 #11

P: 4
Expand|Select|Wrap|Line Numbers
  1. p = new char*[50]; // p = new *char[50]; will give you a syntax error
  2. *p = new char[50];
  3.  
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?
Apr 18 '08 #12

Ganon11
Expert 2.5K+
P: 3,652
Exactly .
Apr 18 '08 #13

Post your reply

Sign in to post your reply or Sign up for a free account.