3

I understand that in C programming, 'scanf' is used with '&' for all variable tyles (int, float, char, ..) except string. Here is my program code. In front of 'scanf', why isn't '&' needed? And may I know more about scanf?

#include <stdio.h>
#define M 10

int main()
{
    int i, n, sum = 0;
    int a[M];

    do{
        printf("Input a positive number less than %d \n",M);
        scanf("%d", &n);
    }while (!(n >0 && n < M));

    printf("Input %d numbers \n",n);

    for(i=0; i<n ; i++){
        scanf("%d",(a+i));
        sum += *(a+i);
    }

    printf("Sum = %d \n",sum);
}
gsamaras
  • 71,951
  • 46
  • 188
  • 305
moh
  • 37
  • 1
  • 5
    `a + i` is an address; `*(a + i)` is a value. If you want to use the `&` you can do: `scanf("%d",&a[i]);`, which I think most people would prefer. – Fiddling Bits Dec 10 '19 at 14:49
  • `a` is an array... `scanf("%d",(a+i));` or `scanf("%d",&a[i]);`...BTW youi should start studing [tag:c] before to start thinking something like _I understand that in C programming, 'scanf' is used with '&' for all variable tyles (int, float, char, ..)_ – LPs Dec 10 '19 at 14:51
  • 2
    @FiddlingBits Beauty is in the eye of the beholder, but I for one could never understand why one would dereference a pointer to obtain an object (`a[i]`) only so that one can get the pointer to the object which one just obtained by *dereferencing that very pointer.* `&a[i]` is `&(*(a+i))` which is obviously nonsensical. – Peter - Reinstate Monica Dec 10 '19 at 15:08
  • @Peter-ReinstateMonica End result (machine code) is probably identical either way. I think clarity is what is most important. – Fiddling Bits Dec 10 '19 at 15:12
  • @FiddlingBits I think less redundancy would be clearer. – Peter - Reinstate Monica Dec 11 '19 at 06:25

3 Answers3

5

Because you have declared a as an array, an expression using that variable's name, on its own, will usually 'decay' to a pointer to the first element of the array (What is array decaying? - but see also the excellent comment added by Eric Postpischil for exceptions). This is similar to using a char[] string where, as you correctly noted, you don't need to use the & operator when passing it as an argument to scanf.

Adding i to this 'base address' of the a array will give the address of (i.e. a pointer to) the 'i'th element of the array.

Here is a decent tutorial on pointer arithmetic in C that you may find useful.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • Then why do I need to add '&' at the code 'scanf("%d", &a[i])'? – moh Dec 10 '19 at 15:10
  • 1
    `(a + i)` will return an address of the element at the `i` index, whereas `a[i]` will return the value at the `i` index. Thus, you need `&` to get the address of the value. – Frontear Dec 10 '19 at 15:14
  • The name of an array, on its own, is not a pointer to the first element of the array. The name of an array, on its own, designates the array. When it is used in an expression, but not as the operand of `sizeof` or unary `&`, then the array (not its name, technically) is automatically converted to be a pointer to its first element. While this may be the bulk of the cases in which an array is used in an expression, teaching the people this wrong statement about arrays causes them to make other errors, which we see repeatedly in other Stack Overflow questions. Please teach the correct rule. – Eric Postpischil Dec 10 '19 at 18:05
  • @EricPostpischil An excellent point, Eric - and thanks for making it! I've updated my answer to address (pardon the pun) the issue - hope you approve. – Adrian Mole Dec 10 '19 at 18:16
2

scanf usually takes the address of a variable it's reading into. a is already an address (an array that's decayed to a pointer) and i is simply an offset from that address.

This is equivalent to scanf("%d", &a[i]);; it does the same thing.

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
1

The & operator used before a variable in c returns the address of that variable. The base of an array already is the address you want. a+i like you're doing is using pointer arithmetic and changing the base address of the array by i