-1

When we using '&' operator in scanf, do we scan address or exact value in that adress? For instance i don't understand how this 2 code give to us same result.

CODE 1

#include <stdio.h>

int main(){ 
    int arr[6], i, sum=0;
    for(i=0;i<6;i++){
        scanf("%d", &arr[i]);
        sum+=arr[i];
    }
    printf("%d", sum);  
}

CODE 2

#include <stdio.h>

int main(){ 
    int arr[6], i, sum=0;
    for(i=0;i<6;i++){
        scanf("%d", (arr+i));
        sum+=*(arr+i);
    }
    printf("%d", sum);  
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
obradovic
  • 21
  • 1
  • 6
  • This is not related to scanf. Note that &arr and arr give the same value. https://stackoverflow.com/a/2528328/13559733 – Aleksander Bobiński Mar 26 '21 at 17:19
  • 1
    The expression `X[Y]` is equivalent to `*(X+Y)` then `&arr[i]` becomes `&*(arr+i)` which is the same as `arr+i`. (Does that mean that `X[Y]` can also be written as `Y[X]`? Yes.) –  Mar 26 '21 at 17:21
  • `arr[i]` is equivalent to `*(arr+i)`, so `&arr[i]` is equivalent to `&*(arr+i)`. The `&` and `*` sort of cancel out, so `&arr[i]` is equivalent to `(arr+i)`. In both cases, a pointer to the *i*-th element of `arr` is passed to `scanf`. – Ian Abbott Mar 26 '21 at 17:21
  • @AleksanderBobiński when passed to functions. as expressions they are different. `&arr` is an `int*` expression, whereas `arr` is an `int[6]` expression. – Caleth Mar 26 '21 at 17:26
  • `scanf` "scans" stdin; the address argument is the _destination_ for the converted input, not what is being "scanned". – Clifford Mar 26 '21 at 17:39

3 Answers3

2

scanf() requires you to pass it the address of each variable that you want it to write parsed values to. The & operator returns those addresses.

In your examples, &arr[i] and (arr+i) both represent the same address of the i'th array element, and also arr[i] and *(arr+i) both represent accesses to that same element's stored value.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0

& is the address-of operator, i.e. given an object, it gives you a pointer to that object.

Along with ordinary arithemetic, operator+ applied to a (pointer, integer) pair gives you a pointer to another element of the same array (so long as there are such elements) or a pointer one past the end of the array (if the value is exactly distance to the end) or undefined behaviour (if you would be out of range).

a[i] is defined as *(a + i). It dereferences the calculated pointer.

Putting that together, arr + i is an int* expression, whereas arr[i] is an int expression. As we need an int* for scanf, & is used to get one.

Caleth
  • 52,200
  • 2
  • 44
  • 75
0

From the C Standard (6.5.2.1 Array subscripting)

2 A postfix expression followed by an expression in square brackets [] is a subscripted designation of an element of an array object. The definition of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2))). Because of the conversion rules that apply to the binary + operator, if E1 is an array object (equivalently, a pointer to the initial element of an array object) and E2 is an integer, E1[E2] designates the E2-th element of E1 (counting from zero).

So the expression &arr[i] is equivalent to &( *( arr + i ) ). The operators * and & applied to the expression arr + i sequentially may be omitted and you will get that &arr[i] is equivalent tp arr + i. The both expressions are pointers to the i-th element of the array arr.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335