-2

I'm starting to learn C and i noticed that sometimes we use

scanf("%d", &x[i]) 

and sometimes it's left as simple:

scanf("%d", x[i])

Can someone please explain the difference to me? I don't understand when we have to put & and when not to use it. Also I don't understand when we have to use *x? and what does it do?

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
JustAsking
  • 27
  • 6

1 Answers1

0

The reason to use that & is to get a pointer. scanf always needs pointers, because it's going to use it to give you a value back. But & is not the only way to get a pointer.

If you have a single int, char, long, float, or double variable, you will need to use a & to get a pointer of it to pass to scanf:

int i;
scanf("%d", &i);
char c;
scanf(" %c", &c);
long l;
scanf("%ld", &l);
float f;
scanf("%f", &f);
double d;
scanf("%lf", &d);

If you are reading a string, however, you usually do not need an &:

char str1[10];
scanf("%9s", str1);
char *str2 = malloc(20);
scanf("%19s", str2);

The reason you do not need a & in either of these cases is that you already have a pointer, although a full explanation of this fact will require more words than I suspect I'll have time for in this answer.

This ends up being one of the reasons why scanf is a terribly problematic function. scanf is most useful during your first few weeks as a C programmer, when you have a lot of different things to learn, and scanf seems like an easy way to get user-entered values in to your program for it to work on. But you can't really understand how to call scanf without understanding pointers, and pointers aren't something you're going to want to learn about until later.

So at first, a reasonable set of rules for successful scanf use is:

if your format is you should:
%d, %f, %lf use an &
%c use an &, and also use an extra space: " %c"
%s not use an &

See also this answer.

Steve Summit
  • 45,437
  • 7
  • 70
  • 103
  • Okay, i understood, thank you! but for example when do we use * instead of &? it was part of my question too – JustAsking Nov 21 '21 at 19:24
  • @JustAsking I'd say, hang on before worrying about `*`. That's part of using pointers yourself — but if you're just starting, there are some other things you're going to want to get under your belt before you attack full-blown pointers. – Steve Summit Nov 21 '21 at 19:39
  • The short answer is that you would never use `*` "instead of" `&`. When it comes to pointers, `&` and `*` are inverse operators. If you have a variable or other object, `&` gives you a pointer to that object. If you have a pointer, `*` gives you back the value of the object that the pointer points to. – Steve Summit Nov 21 '21 at 19:41
  • thank you sooo much for the amazing explanations :D i finally get it. We only learned the basics, they didn't explain to us how to use pointers but they told us about them in exercises. I'll use what you explained to me until we understand more in depth about pointers! Thank you a lot once again for your time and for explaining !! you saved me xd – JustAsking Nov 21 '21 at 19:54