Changing a variable passed by reference and returning it from a function

Following the advice of the C FAQ on creating a matrix instance using a double pointer , I came across another problem. I managed to solve the problem, but I hardly understand why it works in one direction, but not in the other.

I have the following code that works, and then I will show you a bit that does not work, and I hope you can explain why:

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

int HEIGHT = 20;
int WIDTH = 20;

int ** curr_grid;

/**
 * Generate an array with the given height and length
 */
int ** create_grid() {
    int ** grid;
    grid = malloc(sizeof(int *) * HEIGHT);
    int row;
    for (row = 0; row < HEIGHT; row++) 
        grid[row] = malloc(sizeof(int) * WIDTH);
    return grid;
}

/* Entry Point main */
int main(int argc, char** argv) {
    curr_grid = create_grid();
    curr_grid[0][0] = 0;
    free(curr_grid); // Release heap resources
    return 0;
}

Allocates memory for the grid and returns a pointer this way. However, I first tried another method, which, as I was convinced, should work, too, but this is not so:

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

int HEIGHT = 20;
int WIDTH = 20;

int ** curr_grid;

/**
 * Generate an array with the given height and length
 */
 create_grid(int ** grid) {
    grid = malloc(sizeof(int *) * HEIGHT);
    int row;
    for (row = 0; row < HEIGHT; row++) 
        grid[row] = malloc(sizeof(int) * WIDTH);
}

/* Entry Point main */
int main(int argc, char** argv) {
    create_grid(curr_grid);
    curr_grid[0][0] = 0; // Segmentation Fault
    free(curr_grid); // Release heap resources
    return 0;
}

This method completes with a segmentation error on the specified line. However, I got the impression that passing a variable by reference allows you to modify it.

?

+3
3

, create_grid(curr_grid);   , curr_grid ( ), . , .

, .

create_grid(&curr_grid);

: ,

+2

:

void create_grid(int ***grid) {
    *grid = malloc(sizeof(int *) * HEIGHT);
    int row;
    for (row = 0; row < HEIGHT; row++) 
        (*grid)[row] = malloc(sizeof(int) * WIDTH);
}

/* Entry Point main */
int main(int argc, char** argv) {
    create_grid(&curr_grid);
    curr_grid[0][0] = 0;
    free(curr_grid); // Release heap resources
    return 0;
}

:

int MyFunction()
{
  return 3 ;  // we return directly 3
}

void main()
{
  int a ;
  a = MyFunction() ;    
}

void MyFunction1(int *pa)
{
  *pa = 3 ;   // we assign 3 to the memory location pointed by pa
}

void main2()
{
  int a ;
  MyFunction1(&a) ;  // we pass the pointer to a /
  // now a contains 3
}
+1

, ( , ):

void create_grid(int **grid) {
    grid = malloc(...);
    ...
}

grid, grid = malloc(...);, grid - *,   , grids, .

:

void create_grid(int ***grid) {
    *grid = malloc(...);
    ...
}

Now, when you access the grid value using and '*', you know that any changes made this way will remain after the function returns, since the grid is really a reference to your two-dimensional array.

It is simple (regardless of the type of variable).

0
source

All Articles