Solved

use MALLOC function on array

Posted on 2000-03-05
7
224 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Easy, flexible multimedia distribution & control

Coming soon!  Ideal for large-scale A/V applications, ATEN's VM3200 Modular Matrix Switch is an all-in-one solution that simplifies video wall integration. Easily customize display layouts to see what you want, how you want it in 4k.

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

792 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