Code: Select all
char *var1;
var1="test1";
printf("%d, %s",var1,var1); // is correct
printf("%s",*var1); // crashes why??
Code: Select all
char *var1;
var1="test1";
printf("%d, %s",var1,var1); // is correct
printf("%s",*var1); // crashes why??
var1 is a pointer. %d prints the value of the parameter, %s prints the contents (IE, it dereferences). The first printf thus prints the pointer and its content. The second takes the content of the pointer and tries to dereference that, which isn't a pointer. So, it crashes, since "test" is't a valid pointer when cast to a char *.Neo wrote: What is wrong with this?Code: Select all
char *var1; var1="test1"; printf("%d, %s",var1,var1); // is correct printf("%s",*var1); // crashes why??
Code: Select all
char *var1="test1";
Code: Select all
char t = *var1;
Code: Select all
/home/pype> gcc -Wall -c test.c
test.c: In function 'test':
test.c:9: warning: format '%d' expects type 'int', but argument 2 has type 'char *'
test.c:10: warning: format '%s' expects type 'char *', but argument 2 has type 'int'