I am fairly new to C and I am having trouble using scanf with pointers. I have been told to get user inputs for 3 int and 1 char values and then print them back out using pointers.
This is the best I could come up with so far:
int a, b, c;
char d;
int *x = &a;
int *y = &b;
int *z = &c;
char *e = &d;
scanf("Enter 3 Ints and 1 Char:%d %d %d %c", x, y, z, e);
printf("The numbers are:\n");
printf("%d\n %d\n %d\n %c\n", a, b, c, d);
return 0;
When I enter the values the following is printed out:
2 3 4 c
The numbers are:
32708
-613084440
32708
�
Again, I'm very new to programming so I apologize if this is a stupid mistake or something obvious that I have missed.
scanf("Enter 3 Ints and 1 Char:%d %d %d %c", &a, &b, &c, &d);in the first place? Is there any particular reason why you create those pointer variables first?scanfformat string. That won't work. Yourscanfwill only work if you literally typeEnter 3 Ints and 1 Char:at the start of your input, which isn't what you want. Also, check and print the return value fromscanf. That will tell you how many values it actually read. Always check the return value fromscanf, and take appropriate action if it's not what you expect. Don't just ignore it.