We help IT Professionals succeed at work.

# use MALLOC function on array

on
270 Views
Greetings,

I am trying to use MALLOC function to set the memory location for array which is defined by pointer to pointer. The code doesn't has error, but it couldn't run. The part of code is attached as following:

int i,j;
int **matrix;

main()
{
matrix=(int**)malloc(5*5*sizeof(int));

/*initialize the array.*/
for (i=0; i<=4; i++)
for(j=0;j<=4;j++)
*(*(matrix+i)+j)=0;
}

Is there anything wrong with the usage of MALLOC? The code stop at
*(*(matrix+i)+j)=0

-Zheng
Comment
Watch Question

## View Solution Only

CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
matrix=(int**)malloc(5*sizeof(int*));

/*initialize the array.*/
for( i=0; i<=4; i++ ){
matrix[i] = (int *)malloc(5*sizeof(int));
for(j=0;j<=4;j++){ *(*(matrix+i)+j)=0; }
}

Commented:

(int *)matrix= malloc(5*5*sizeof(int));
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)

Commented:
ozo's solution works.

ufolks123's solution probably works because it is very similar to ozo's.

Zheng, your way does not work because you have not informed the compiler that matrix has the form of a [5][5] array, so it does not know what units i is specified in when you say "matrix + i".

For interest, here is a solution whose main advantage is that it uses a single malloc:

#include "malloc.h"
#define NUM_OF_ROWS      5

typedef int    matrixRow[5];

main()
{
int i,j;
matrixRow  *matrix;

matrix = malloc(NUM_OF_ROWS * sizeof(matrixRow));

/*initialize the array.*/
for (i=0; i<=NUM_OF_ROWS; i++)
for(j=0;j<=4;j++)
*(*(matrix+i)+j)= 0;
}

This could be changed to use ufolk123's [][] matrix addressing if required.

Commented:
In the previous post, change
i<=NUM_OF_ROWS   to i<NUM_OF_ROWS

Commented:
Thank you very much for you inputs.
-Zheng

Commented:
Hi zhu_zheng

Welcome to Experts Exchange.

I am interested to know why you chose ufolks answer rather than that of another contributor.
Was it the first one that worked?
Was it the best one?
Was it highly original or elegant?

You should not necessarily award the points to the first person who posts an "answer".  You choose the one that YOU want.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

###### Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

Deciding to stick with EE.

Mohamed Asif

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
###### Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Connect with Certified Experts to gain insight and support on specific technology challenges including:

• Troubleshooting
• Research
• Professional Opinions
Unlock the solution to this question.

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.