the question about char pointer

why i need to put & before ch in printf string?
and i don"t need to put & before ch in printf char?


1
2
3
4
5
6
7
8
9
10
11
#include<stdio.h>
int main(){
	char *ch;
	scanf("%s",&ch);
	printf("%s",&ch);
}
int main(){
	char *ch;
	scanf("%c",&ch);
	printf("%c",ch);
}
Last edited on
why i need to put & before ch in printf string?

You don't need the ampersand in either the scanf() or printf() calls, your compiler should be warning you about these issues in the code you posted.


In function 'int main()': 4:16: warning: format '%s' expects argument of type 'char*', but argument 2 has type 'char**' [-Wformat=] 5:17: warning: format '%s' expects argument of type 'char*', but argument 2 has type 'char**' [-Wformat=]

By the way the "%s" format specifier expects a pointer to an array of char. And where are you allocating/assigning memory for that uninitialized pointer?

thx bro
Topic archived. No new replies allowed.