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

Help me with pointer to pointers

P: 25
I'm working on dynamic arrays. I found source code in C. Please help me convert this line of code from C to C++. Thanks so much for the help.

row and col are variables

Expand|Select|Wrap|Line Numbers
  1. arr=(int**)malloc(sizeof(int)*row*col);
Mar 13 '07 #1
Share this Question
Share on Google+
7 Replies

P: 39
well if you are truly interested in learning on how to create dynamic arrays in C++, I would suggest researching STL containers . Creating dynamic arrays, such as vectors, lists, deques...etc is much safer and is usually efficient enough for most general purposes. Plus comes with lots of little functions like sort or insert.
Mar 13 '07 #2

Expert 2.5K+
P: 3,652
Since the OP is working on dynamic arrays alone, the STL stuff is probably in the future for his/her coursework (I'm assuming you're taking a class).

C++ uses the new operator, so the malloc call you have should be

Expand|Select|Wrap|Line Numbers
  1. int** arr = new int[rows][cols];
in C++.
Mar 13 '07 #3

P: 25
Yeap, I'm taking CS 161, really basic stuff, this is my extra credit project. That's y my instructor wanted us to write functions, should not use vector at this time. Anyways, thanks much for the help!
Mar 14 '07 #4

P: 25
When I tried put that code in the function, it gave me some errors such as:
==> declaration of 'int**arr' shadows a parameter
==> `col' cannot appear in a constant-expression

How can I fix this errors?

Expand|Select|Wrap|Line Numbers
  1. /* to initialize matrix */
  2. int** init(int** arr,int row,int col)
  3. {
  4.                int i=0,
  5.                    j=0;
  7.     //arr=(int**)malloc(sizeof(int)*row*col);
  8.     int** arr = new int[row][col];
  10.     for(i=0;i<row;i++)
  11.     {
  12.         for(j=0;j<col;j++)
  13.         {
  14.             *((arr+i)+j)=(int*)malloc(sizeof(int));
  15.             *(*(arr+i)+j)=0; 
  16.         }
  17.     }
  18.     return arr;
  19. }
Mar 14 '07 #5

Expert 100+
P: 1,510
try replacing
Expand|Select|Wrap|Line Numbers
  1.     int** arr = new int[row][col];
Expand|Select|Wrap|Line Numbers
  1.      arr = new int*[row];
  2.      for(int i=0;i<row;i++)
  3.        arr[i]=new int[col];
Mar 14 '07 #6

P: 25
The compiler of BloodShed did not warning for errors but program terminate when accessing the function with that replacing. Any ideas? Many thanks for the help so far.
Mar 14 '07 #7

P: 8
I suggest if u want to create dynamic 2D array

int rows = 4;
int cols = 3;

int **pp = new int*[rows];

for ( int i =0 ; i < rows ;i ++ )
pp[i] = new int[cols];

This will create a dynamic 2D array of rows 4 and cols 3.

can access the array element using pp[i][j].
Mar 14 '07 #8

Post your reply

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