+ 3
Why this code output nothing ?? Help plz
#include<stdlib.h> #include<stdio.h> #include<string.h> int main(){ char *str ="12345"; printf("%s",*str); return 0; } // output : nothing why ?
4 Antworten
+ 8
Mik_RDC the %s format specifier needs a pointer. Remove the dereference and it will work.
Change:
printf("%s",*str);
To:
printf("%s",str);
+ 3
Mik_RDC I'll give a short backgrounder that might clarify:
The character string "12345" is stored at some location in memory. The location has a unique numeric address.
If we store the address in a variable, p, then p is a pointer to the data. p has its own memory location just like an integer variable. If you print the contents of p as a numeric value, it shows the memory address of "12345". This is the pointer value that %s needs to locate the string.
For other purposes use *p to access the data that p refers to. This is called "dereferencing the pointer". For instance, in printf("%c", *p), the specifier %c prints a single character. It expects a character value, not an address like %s does, so *p is appropriate. It prints the first character of the string, which is '1'.
+ 3
Big thanks bro Brian
+ 2
Brian * (asterix) is'nt use to access pointer value ?