Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

use MALLOC function on array

Posted on 2000-03-05
7
Medium Priority
?
229 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 320 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
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!

 
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

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!

Question has a verified solution.

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

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…
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 opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

704 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