0

In this program I have char variable a, b is a pointer to a and c is a pointer to b. While *a=b, *c not equal to b. I don't understand why ,Can anyone explain?

Another thing I don't understand I that if I change variable from char to int, dereference c result b value. *c equal to b.But if variable is char type, it does not.

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

int main()
{
char a =  "a" ;
char *b;


b = &a;
printf("%d\n", b);
printf("%d\n\n", &a);
printf("Deference b* hold value: %d\n", *b);
printf("a hold value: %d\n\n", a);
char *c;
c = &b;
printf("%d\n", c);
printf("%d\n\n", &b);
printf("Deference *c hold value: %d\n", *c);
printf("b hold value: %d\n\n", b);// why *c not equal b
return 0;

}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
J Nguyen
  • 113
  • 1
  • 13
  • 2
    `char a = "a" ;`...turn up compiler warnings.... – Sourav Ghosh Apr 10 '17 at 10:04
  • 1
    Single pointers are meant to hold the address of another variable. If you want to hold or point to a pointer you need a double pointer. – Gaurav Pathak Apr 10 '17 at 10:06
  • 1
    `*a=b`...what do you mean? – Sourav Ghosh Apr 10 '17 at 10:11
  • @SouravGhosh `char **c = &b` – Gaurav Pathak Apr 10 '17 at 10:37
  • the posted code does not cleanly compile. When compiling, always enable the warnings. then fix those warnings. (for `gcc`, at a minimum use: `-Wall -Wextra -pedantic` I also use: `-Wconversion -std=gnu11` ) On my computer: `gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4)` this results in 10 warnings, several of which are serious. When code does not cleanly compile, there is no assurance as to what is actually happening. Suggest fix the code, then try again, starting with compiling with the warnings enabled. – user3629249 Apr 10 '17 at 15:02

2 Answers2

4

First of all,

 char a =  "a" ;

is illegal, you're essentially trying to store a pointer into a char. What you need is

char a =  'a' ;

Then, saying

printf("%d\n", b);
printf("%d\n\n", &a);  //and all later pointer related prints....

causes undefined behavior as you're passing wrong type of arguments to %d. To print pointers, you need to

  • use %p format specifier.
  • cast the argument to void*

After that,

char *c;
c = &b;

is also wrong, see the data types. &b is a pointer to pointer-to-char. That is not the same as char *, as you have assummed. You need c to be of type char **.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 1
    `char a = "a"` is not illegal. My compiler lets me do it but emits a warning because it compiles but is clearly wrong. I expect there is UB in there somewhere though. – JeremyP Apr 10 '17 at 10:16
  • @JeremyP it should be a constraint violation, please correct me if I'm wrong. – Sourav Ghosh Apr 10 '17 at 10:19
  • clang compiles it even with `-std=c11`. If it were illegal (in the strictest sense), I'd expect to see an error not a warning. – JeremyP Apr 10 '17 at 10:26
  • 3
    It is complete nonsense though. – JeremyP Apr 10 '17 at 10:28
  • 2
    @JeremyP It is a constraint violation of the rules for simple assignment. The compiler must generate a diagnostic. You must use the compiler in standard C mode, `-std=c11 -pedantic-errors`. If you use it in non-standard mode, it allows all manner of weird crap to compile. – Lundin Apr 10 '17 at 11:12
  • (Where "pedantic errors" doesn't mean "be overly pedantic", but rather "don't be non-standard crap". gcc of course wishes to market its non-standard crap as the norm, pretending that standard C is something exceptional, hence the name of the compiler option.) – Lundin Apr 10 '17 at 11:14
  • Hi, as you advice, i change variable type of c to char **c and it works. * c equal to b now. However if i change all variables from char to int. For example: int *c and assign c = &b. I print *c and b and they both hold the same value. Can you explain that for me. Thanks. – J Nguyen Apr 10 '17 at 12:42
  • 1
    @JNguyen as mentioned earlier, there's no possible explanation that holds true universally, simply don't do it. Constraint violation invokes UB....data types are there for a reason, right? :) – Sourav Ghosh Apr 10 '17 at 12:44
0

Look at the compiler warnings, maybe you want this:

int main()
{
    char *a = "a";
    char *b;    

    b = a;
    printf("%p\n", b);
    printf("%p\n\n", &a);
    printf("Deference b* hold value: %d\n", *b);
    printf("a hold value: %p\n\n", a);
    char *c;
    c = b;
    printf("%p\n", c);
    printf("%p\n\n", &b);
    printf("Deference *c hold value: %d\n", *c);
    printf("b hold value: %p\n\n", b);// why *c not equal b
    return 0;    
}
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115