简体   繁体   中英

Why doesn't my function work to adjust my pointer's content, unless i return one to assign it

My question is what is the difference between these two? one is a void, and another one returns a 2d array, they however do the same but the functionality doesn't seem to do the same? i must be misunderstanding pointers here.

I thought pointers stored nothing but an adress to point to, so if i pass one as a parameter, and change the contents and to where it points, don't i do the same as re-assigning it to the functions return value.

on the end of both functions we print the first line, they did so on both. but whilst printing the adjusted grid by the void function in my Main i get a segfault.

char    **ft_grid_gen(int size)
{
    char    **map;
    int     index;
    int     elem_index;
    int     sq_root;

    index = 0;
    elem_index = 0;
    sq_root = ft_sqrt(size * 4);
    map = (char**)malloc(sq_root * sizeof(char *));
    while (index < sq_root)
    {
        map[index] = (char*)malloc(sq_root * sizeof(char));
        while (elem_index < sq_root)
        {
            map[index][elem_index] = '.';
            elem_index++;
        }
        index++;
        elem_index = 0;
    }
    printf("GENERATED NEW GRID of size %s!\n", map[0]);
    return (map);
}
void    ft_grid_gen(char **map, int size)
{
    int     index;
    int     elem_index;
    int     sq_root;

    index = 0;
    elem_index = 0;
    sq_root = ft_sqrt(size * 4);
    map = (char**)malloc(sq_root * sizeof(char *));
    while (index < sq_root)
    {
        map[index] = (char*)malloc(sq_root * sizeof(char));
        while (elem_index < sq_root)
        {
            map[index][elem_index] = '.';
            elem_index++;
        }
        index++;
        elem_index = 0;
    }
    printf("GENERATED NEW GRID of size %s!\n", map[0]);
}

Note that ft_grid_gen don't just change that to which map points; it also change map itself.

Changing map has no more effect on the caller than changing size . Whatever variable you used as a parameter in the caller still point to "nowhere useful" after ft_grid_gen .

If you want to return a value via a parameter, you need to pass a pointer to the variable that will receive the value.

void f(int *i_ptr) {
   *i_ptr = 123;
}

int i;
f(&i);

In your case, this would be

void ft_grid_gen(char ***map_ptr, int size) {
   ...
   *map_ptr = map;
}

char **map;
f(&map);

Alternatively, you could allocate the memory on the outside. Since the function no longer has a need to change map , the value of map can be passed directly.

void ft_grid_gen(char **map, int size) {
   ...
}

char **map = malloc(ft_sqrt(size * 4) * sizeof(char*));
ft_grid_gen(map, size);

(Obviously, this approach is far from ideal in this specific situation.)

The difference is that the first function returns something you can use later on. In the second function, you pass in a char** by value, and then with:

map = (char**)malloc(sq_root * sizeof(char *));

You assign a new value to the local variable map , which was assigned its first value through the function parameter. However this does not affect the original char** variable in main() -- because it was passed in by value like everything else in C. If you wanted to change the main() variable, you would have passed a pointer to it (ie char*** ) and de-referenced it in this function, like:

*map = (char**)malloc(sq_root * sizeof(char *));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM