简体   繁体   English

将指针的值更改为ac函数中的指针

[英]changing the value of pointer to a pointer in a c function

I am passing in a double pointer to a function then the function allocated space on the heap and redirects the double pointer to that memory location. 我传入一个函数的双指针,然后函数在堆上分配空间,并将双指针重定向到该内存位置。 then in the main we should be able to access the memory location and its content 然后在主要我们应该能够访问内存位置及其内容

my code: 我的代码:

void grab_letter(char **s){
   char *a = (char *) malloc(sizeof(char));
    *a = 'r';
    s=&a;
}

int main(){
    char **s;
    grab_letter(s);
    printf("returned:%c\n",**s);
}

when i use gdb to check the returned value of s after the grab_letter call, it's 0x0. 当我使用gdb在grab_letter调用之后检查s的返回值时,它是0x0。

Can someone help me on what i am misunderstanding here?:) thanks 有人可以帮助我解决我在这里的误解吗?:)谢谢

You have to pass a pointer to a pointer, which you are doing, but your pointer s is not pointing to any valid memory. 您必须将指针传递给您正在执行的指针,但指针s未指向任何有效的内存。

void grab_letter(char **s){
   char *a = malloc(sizeof(char));
    *a = 'r';
    *s=a;    //dereference the pointer to pointer s and point it to a
}

int main( void ){
    char *s;
    grab_letter(&s);   //pass the address of pointer
    printf("returned:%c\n",*s);
}

Rather pass an address of the pointer s. 而是传递指针s的地址。 Double pointer s now points to original pointer s in the main. 双指针现在指向主要的原始指针s。 Then just dereference it and point it to a. 然后只需取消引用它并指向它。

There are several problems here. 这里有几个问题。 One is that main declares a doubly indirect pointer. 一个是主要声明一个双重间接指针。 Singly indirect would be good enough and more to the point. 单一的间接将是足够好的,更重要的是。 Even more to the point is not to declare a pointer, but pass a simple character variable as a parameter: 更重要的是不要声明指针,而是将一个简单的字符变量作为参数传递:

int main()
{
    char s;
    grab_letter(&s);
    printf("returned:%c\n", s);
}

With that changed, there need not be any malloc() and the parameter handling is quite straightforward: 随着更改,不需要任何malloc()和参数处理非常简单:

void grab_letter(char *s)
{
    *s='r';
}

If you really want to use a doubly indirect pointer, this will do it; 如果你真的想使用双重间接指针,那就可以了;

void grab_letter(char **s)
{
    *s = malloc (sizeof (char));
    **s = 'r';
}

int main()
{
    char *s;
    grab_letter(&s);
    printf("returned:%c\n", *s);
}

This presumably performs the form of logic you had in mind. 这可能是你想到的逻辑形式。

char **s;

The above code delcares s as a pointer to a char pointer, which isn't what you want. 上面的代码delcares s作为指向char指针的指针,这不是你想要的。 Instead you want: 相反,你想要:

char *s;
grab_letter(&s);

grab_letter is expecting a pointer to a pointer, so you need to pass to it the address of a pointer. grab_letter期待指向指针的指针,因此您需要将指针的地址传递给它。

void grab_letter(char **s){
   char *a = (char *) malloc(sizeof(char));
    *a = 'r';
    s=&a;
}

The problem with the above is that pointer a is on the stack, and you're assigning s to that address which may or may not be valid when the function returns. 上面的问题是指针a在堆栈上,并且您正在为该地址分配s,该函数在函数返回时可能有效,也可能无效。 In addition what you're trying to do is return to the caller the newly malloc'd address, so you should dereference s, as s is a pointer TO a pointer, and then assign it the address stored in a. 此外,您要做的是将新malloc的地址返回给调用者,因此您应该取消引用s,因为s是指向指针的指针,然后为其分配存储在a中的地址。

void grab_letter(char **s){
   char *a = (char *) malloc(sizeof(char));
    *a = 'r';
    *s = a;
}

The above is borrowed from self.'s answer. 以上是借用自我的答案。

C passes in all arguments by value. C按值传递所有参数。 Therefore the s passed into grab_letter is just a value. 因此传入grab_letter的s只是一个值。 In order to change the value of s on the outside you need to assign &a to *s. 要在外部更改s的值,您需要指定&a到* s。

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

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