• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Double pointer in C?

Example:

const int num_rows = 5;
const int num_cols = 5;


int **pMatrix = new int [num_rows];
for(int i=0; i < num_rows; ++i)
{
pMatrix = new int [num_cols];
}


To Delete:
for(int i=0; i < num_rows; ++i)
{
delete pMatrix;
}

delete pMatrix;
 
new and delete are C++ keywords. and it realy should be delete [] in those cases.
also int **pMatrix = new int [num_rows]; should really be = new int*[num_rows];

instead use int **pMatrix = (int**)malloc(sizeof(int*) * num_rows);
then pMatrix(subscripti) = (int*)malloc(sizeof(int) * num_cols);

(i used (subscripti) because forum software interprets square brackets with i as italic)

and free() instead of delete
 
Originally posted by: dighn
new and delete are C++ keywords. and it realy should be delete [] in those cases.
also int **pMatrix = new int [num_rows]; should really be = new int*[num_rows];

instead use int **pMatrix = (int*)malloc(sizeof(int*) * num_rows);

and free() instead of delete

Do I still need to use the for loop to allocate the "inner" array?
 
Originally posted by: johnnytightlips
Originally posted by: dighn
new and delete are C++ keywords. and it realy should be delete [] in those cases.
also int **pMatrix = new int [num_rows]; should really be = new int*[num_rows];

instead use int **pMatrix = (int*)malloc(sizeof(int*) * num_rows);

and free() instead of delete

Do I still need to use the for loop to allocate the "inner" array?

yes you do. i fixed a little typo too
 
Originally posted by: singh
Example:

const int num_rows = 5;
const int num_cols = 5;


int **pMatrix = new int [num_rows];
for(int i=0; i < num_rows; ++i)
{
pMatrix = new int [num_cols];
}


To Delete:
for(int i=0; i < num_rows; ++i)
{
delete pMatrix[ I ];
}

delete pMatrix;


edited to show you the [ i ]
 
Back
Top