Solved

use MALLOC function on array

Posted on 2000-03-05
7
223 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

785 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