简体   繁体   English

为什么我的函数不能调整指针的内容,除非我返回一个指针来分配它

[英]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? 一个是空的,另一个是返回2d数组,但是它们执行相同的操作,但是功能似乎不相同吗? 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. 但是在通过Main中的void功能打​​印调整后的网格时,出现了段错误。

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; 请注意, ft_grid_gen不仅ft_grid_gen更改为map点; it also change map itself. 它还会更改map本身。

Changing map has no more effect on the caller than changing size . 更改map对呼叫者的影响不超过更改size Whatever variable you used as a parameter in the caller still point to "nowhere useful" after ft_grid_gen . 无论您在调用ft_grid_gen用作参数的任何变量仍指向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. 由于函数不再有需要改变map ,价值map可以直接传递。

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: 在第二个函数中,按值传递char** ,然后输入:

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. 您将新值分配给局部变量map ,该局部变量map是通过function参数分配的第一个值。 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: 但是,这不会影响main()的原始char**变量-因为它是像C中的其他所有内容一样通过值传递的。如果您想更改main()变量,则应该将指针传递给它(即char*** ),并在此函数中取消引用它,例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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