1

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.

2
  • 1
    Why don't you just do 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? Commented Oct 8, 2022 at 10:11
  • 3
    You're trying to include the prompt string in the scanf format string. That won't work. Your scanf will only work if you literally type Enter 3 Ints and 1 Char: at the start of your input, which isn't what you want. Also, check and print the return value from scanf. That will tell you how many values it actually read. Always check the return value from scanf, and take appropriate action if it's not what you expect. Don't just ignore it. Commented Oct 8, 2022 at 10:14

1 Answer 1

3

You are not checking the return value of your scanf, otherwise you would know that it returns 0, as in 'no elements read'.

Your scanf expects you to write exactly what you are putting in there, so, if you entered Enter 3 Ints and 1 Char:2 3 4 c, it would probably work.

What you want, however, is this:

printf("Enter 3 Ints and 1 Char: ");

if (scanf("%d %d %d %c", &a, &b, &c, &d) != 4)
    printf("Invalid input detected\n");
else
    printf("The numbers are:\n%d\n %d\n %d\n %c\n", a, b, c, d);

The first line will print the prompt to the console, the second will read the values into variables.

There is no need to create separate pointer variables for this purpose.

Sign up to request clarification or add additional context in comments.

6 Comments

What do you mean by checking the return values from scanf?
@GrandadDouglas, scanf returns the number of values actually parsed successfully. I have amended the snippet to include 'return value checking' for clarity.
@GrandadDouglas scanf returns an integer, which is the number of fields it successfully scanned. Compare that value to what you expect, and give an error if it doesn't match.
@GrandadDouglas Always look at the man page for a library function before using it. In this case, it shows that scanf returns an int, and it describes what that return value is. Never just ignore errors. That's the road to buggy code that no one wants to use.
Glad I was able to help, @GrandadDouglas. Please don't forget to accept an answer, so others will know that you no longer require help, thank you.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.