0

This week my teacher of C language explained that we can use * (array + i) rather than array [i], he also mentioned the advantages of using this "new method". It's ok so far and then he gave us some exercises to do, in one of them I'm trying to use along with scanf this new method.

I know how to do it like this:

printf("%i", *(array + i));

My doubt is, how can I use *(array + i) in an scanf, or it's not possible?

scanf("%i", *(array + i));


for(i = 0; i < size; i++)
{

    printf("Insert number: %i", i+1);
    scanf("%i", *(array + i); // it's not working.

}
  • 1
    You are aware that scanf needs a pointer at the place where you are now giving a dereferenced pointer, aren't you? – Yunnosch Mar 29 '18 at 20:37
  • do away with the dereference – yano Mar 29 '18 at 20:37
  • besides just knowing the answer to your immediate problem, make sure you understand _why_ `*(array+i)` and `array[i]` are the same thing. They're both performing [pointer arithmetic](https://stackoverflow.com/questions/394767/pointer-arithmetic),, the `array[i]` syntax just has the dereference baked in. – yano Mar 29 '18 at 20:45
  • `scanf("%i", *(array + i);` does not even compile given unbalanced `()`. Posting true code is a good first step in presenting a problem. – chux - Reinstate Monica Mar 30 '18 at 02:48
  • Have you tried? what happened? :) – Luis Colorado Apr 01 '18 at 14:30

4 Answers4

4

The argument to scanf must be a pointer to the variable you want to store the result in, so you shouldn't dereference it:

scanf("%i", array+i);

This is simply the opposite of the way you deal with ordinary variables in printf() and scanf():

printf("%i", var);
scanf("%i", &var);

Adding & is analogous to removing *.

Barmar
  • 741,623
  • 53
  • 500
  • 612
2
*(array + i) ==  array[i]
  array + i  == &array[i]

This means:

printf( "%i", array[i] ) == printf( "%i", *(array + i) );
scanf( "%i", &array[i] ) == scanf( "%i", array + i );

he also mentioned the advantages of using this "new method"

What advantages would those be? In my experience, using *(a + i) over a[i] tends to result in code that's less clear, and easier to get wrong.

John Bode
  • 119,563
  • 19
  • 122
  • 198
  • For example, he said that this method is runs faster than the other... I don't remember exactly why but it is what he said. To be honest I think this new method is harder and unnecessary... But I have to do my homework using it. –  Mar 31 '18 at 11:45
  • Thanks @John Bode –  Mar 31 '18 at 11:50
  • @LelreFerreira: Ugh. Next time he says something like that, ask him (politely!) to show how he measured the performance increase and how big it was. Saving a clock cycle on a task that takes several milliseconds (I/O) is *meaningless*. – John Bode Mar 31 '18 at 14:08
0

In C, a[i] actually gets translated to *(a+i); it's called syntactic sugar

Arrays are referenced using their variable names and individual array elements are referenced as the starting address + (element position*size of each element)

As mentioned in the Barmar's Answer - you should be using

a+i instead of *(a+i)

because the scanf function needs an address as argument to store the input, not the dereferenced value

0

Indeed, you have to use scanf(3) with pointers, as it is not allowed to pass a nonpointer value. But what you cannot do (except if the final pointed value is also a pointer) is to use them with dereferenced pointers (what is indeed done in your example, when you write *(array + i)) If you want to use the pointer you can for example, pass array + i which is a perfectly valid pointer, that points to the i-esim element of array.

Luis Colorado
  • 10,974
  • 1
  • 16
  • 31