Solved

use MALLOC function on array

Posted on 2000-03-05
7
226 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:zhu_zheng
7 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 2586028
 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; }
  }
0
 
LVL 5

Expert Comment

by:PC_User321
ID: 2586036
In your program, try

  (int *)matrix= malloc(5*5*sizeof(int));
0
 
LVL 3

Accepted Solution

by:
ufolk123 earned 80 total points
ID: 2586323
int **matrix;
#include<stdlib.h>

main()
{
  matrix=(int**)malloc(5*sizeof(int*));
  for (i=0; i<=4; i++)
   matrix[i]=(int*)malloc(5*sizeof(int));
   
  /*initialize the array.*/
  for (i=0; i<=4; i++)
   for(j=0;j<=4;j++)
    matrix[i][j]=0;
}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:PC_User321
ID: 2586631
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.
0
 
LVL 5

Expert Comment

by:PC_User321
ID: 2586683
In the previous post, change
i<=NUM_OF_ROWS   to i<NUM_OF_ROWS
0
 

Author Comment

by:zhu_zheng
ID: 2590011
Thank you very much for you inputs.
-Zheng
0
 
LVL 5

Expert Comment

by:PC_User321
ID: 2590068
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.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question