-1

i am a beginner and this question may sound dumb but anyway hope you guys can help. In this program, i have variable char a, b is a pointer to a, I pass address of b to function test. Then c is a pointer of b in function test, and c is pointer of pointer for variable a. My question is : Is there any possible way to dereference c and print out a. I dont want to print out a in main function, i want to do it in function test when test(&b) executed. Is there anyway to do that.

Below is my code (it is not completed cause i cant find the way to dereference c to get a in function test). Thank you in advance.

#include<stdio.h>
#include<string.h>

void test(char *c)
{
    // how can  i get a from test function.
}

int main(void)
{
    char a =  'a' ;
    char *b = &a;
    test(&b);
}
J Nguyen
  • 113
  • 1
  • 13
  • 1
    Oops. My answer was hogwash. Note that the *type* of `&b` is a `char**`. – Bathsheba Apr 10 '17 at 15:45
  • Hi, what i mean is that is there anyway to print out a when test (&b) execute. I would like to keep parameter of test function as char *c. – J Nguyen Apr 10 '17 at 15:49
  • I guess you are again asking the same question. First [question](https://stackoverflow.com/questions/43320263/c-program-why-dereferece-a-char-pointer-to-a-pointer-doesnt-get-expected-value) asked by you. – Gaurav Pathak Apr 10 '17 at 15:52
  • Hi Gaura, it is not really.I just want to explain a little bit. Because im reading the book C how to program of Paul Deitel, it is the text book in my programming course. In Data structure Binary search tree chapter, p535 the author declare a function with prototype of a pointer * (not pointer of pointer **) .However then he pass an address of a pointer in main to that function. And the program still works. That makes me confused. That is why i do some simple code to see how pointer of pointer work. I know my questions sound dumb but sorry for that. I dont intend to spam here. Thank everyone. – J Nguyen Apr 10 '17 at 16:14

2 Answers2

2

c contains the the address of b, so *c is the same as b.

b contains the the address of a, so **c and *b are the same as a.


Note that if you want to continue to pass a pointer to a pointer to a char, you'll need to change the prototype to

void test(char **c)

All together:

void test(char **c)
{
    printf("%c\n", **c);  // a
    **c = 'c';
}

int main(void)
{
    char a = 'a';
    char *b = &a;
    test(&b);
    printf("%c\n", a);  // c
}

Are you sure you need that extra level of indirection?

void test(char *b)
{
    printf("%c\n", *b);  // a
    *b = 'b';
}

int main(void)
{
    char a = 'a';
    test(&a);
    printf("%c\n", a);  // b
}
ikegami
  • 367,544
  • 15
  • 269
  • 518
0

You have a type mismatch.

Since a is a char, &a will be a char* which you can assign to b as b is also a char*. So far so good.

But - when you call the function, you use &b which is a char**. However, the function expects a char* so you have a type mismatch.

All you need is:

#include<stdio.h>
#include<string.h>

void test(char *c)
{
    printf("%c\n", *c);  // Will print the value of a, i.e. 'a'

    *c = 'b';  // Will assign new value to a
}

int main(void)
{
    char a =  'a' ;
    test(&a);
    printf("%c\n", a);  // Will print the value of a, i.e. 'b'
    return 0;
}
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
  • Hi. I just want to make my question clearer. I would like to change value of a when test (&b) executed, given that I dont want to pass &a to function test and i dont want to change paratemter function: void test (char*c). Is there anyway to do that? – J Nguyen Apr 10 '17 at 15:57
  • @JNguyen - The short answer is **No**. Your code has `b` as a `char*` so `&b` is a `char**`. In other words - passing `&b` to a function expecting a `char*` is simply a type mismatch. You can pass `b` (i.e. `test(b)`) but that is pretty meaningless as it is the same as `test(&a)`. – Support Ukraine Apr 10 '17 at 16:01
  • @JNguyen - Maybe you could explain why you have those restrictions. This may be an XY-problem – Support Ukraine Apr 10 '17 at 16:02