+ 1
Output of C code
I don't understand in the following code, why fun(&y) outputs garbage. Can someone explain please? #include <stdio.h> void fun(char *a) { printf("fun() %s\n",a); } int main() { char x[] = "hello"; fun(x); char y = 'a'; fun(&y); return 0; }
1 Respuesta
+ 1
Martin Taylor
Ah, I get it now better. I think the thing that was confusing me was how the format specifiers can cast the output. I was trying to figure out why the char pointer wasn’t working like I expect. Besides forgetting dereferencing, I put %s instead of %c. Below was what I was trying to get at.
#include <stdio.h>
void fun(char *a)
{
printf("fun() %c\n",*a);
}
int main()
{
char x[] = "hello";
fun(x);
char y = 'a';
fun(&y);
return 0;
}