Hi. I have this part of code.

int main() {
  int **table = NULL;
  allocated_matrix(table, 3, 3);
  //next instructions
}

void allocate_matrix((what_type_here)table, int rows, int columns) {
  
  table = (int **) malloc(rows * sizeof(int *));
  
  for (int i = 0; i < rows; i++) {
   table[i] = (int *) malloc(columns * sizeof(int));
  }
}

I want to allocate a matrix in another function(which is allocate_matrix). The thing is the matrix is allocated inside allocate_matrix function, but when it goes back to main() the table variable still has a NULL value, but it should have an address value. I tried this with the type int** instead of (what_type_here). I'm not sure how to do it so it works properly.

Thanks in advance.

Edited 7 Years Ago by Placinta: n/a

Here's an example of passing/allocating a pointer in a function

#include <stdio.h>
#include <stdlib.h>

void Test(char **cptr);

int main()
{
	char *str;

	Test(&str);

	fputs(str, stdout);
	free(str);
	return(0);
}

void Test(char **cptr)
{
	int val = 0;

	printf("How many characters do you want to enter->");
	scanf("%d", &val);
	getc(stdin);

	*cptr = (char*) malloc(val * sizeof(char));

	printf("Enter your text: (DO NOT INCLUDE SPACES)->");
	fgets(*cptr, val, stdin);
}

To change the value of an object in a called function, you generally pass a pointer to the object.

#include <stdio.h>
#include <stdlib.h>

void allocate_matrix(int ***table, int rows, int columns);

int main()
{
   int i, j;
   int **table = NULL;
   allocate_matrix(&table, 3, 3);
   for ( i = 0; i < 3; ++i )
   {
      for ( j = 0; j < 3; ++j )
      {
         printf("table[%d][%d] = %d\n", i, j, table[i][j]);
      }
   }
   return 0;
}

void allocate_matrix(int ***table, int rows, int columns)
{
   int i, j;
   *table = malloc(rows * sizeof **table);

   for ( i = 0; i < rows; ++i )
   {
      (*table)[i] = malloc(columns * sizeof *(*table)[i]);
      for ( j = 0; j < columns; ++j )
      {
         (*table)[i][j] = i * columns + j + 1;
      }
   }
}

/* my output
table[0][0] = 1
table[0][1] = 2
table[0][2] = 3
table[1][0] = 4
table[1][1] = 5
table[1][2] = 6
table[2][0] = 7
table[2][1] = 8
table[2][2] = 9
*/

You could also do something like this. Where you return the memory address of the allocate space in heap.

#include <stdio.h>

int** allocate_matrix(int rows, int columns);

int main() 
{  
    int **table = NULL;  
    
    table = (int **)allocate_matrix(3, 3);  
    
    getchar();
    return 0;
}

int** allocate_matrix(int rows, int columns) 
{
     int i, **table;
     
     table = malloc(rows * sizeof(int *) );
  
     for (i = 0; i < rows; i++) 
         table[i] = malloc(columns * sizeof(int));
         
     return table;
}

And of course, dont forget to do some error checking there! Which is very important

~ssharish

Edited 7 Years Ago by ssharish2005: n/a

Thank you very much, Dave Sinkula. That was just what I wanted to figure out. All the dereferencing got me confused.

Thanks ssharish2005, but I figured out how to do it by returning, I was more interested in passing the variable by reference.

This article has been dead for over six months. Start a new discussion instead.