Member Avatar for Placinta

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.

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

Member Avatar for Placinta

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.