Solved

use MALLOC function on array

Posted on 2000-03-05
7
220 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now