简体   繁体   中英

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.

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.

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. Double pointer s now points to original pointer s in the main. 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:

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. 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.

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. 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.

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. Therefore the s passed into grab_letter is just a value. In order to change the value of s on the outside you need to assign &a to *s.

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