0

Isn't i in scanf and in sum useless? It doesn't change anything, even when I print &ptr it doesn't show me any difference in memory values.

#include <stdio.h>
#include <stdlib.h>

int main() {
    int n, *ptr, sum = 0;

    printf("Enter number of elements: ");
    scanf("%d", &n);

    ptr = (int *)malloc(n * sizeof(int));

    // if memory cannot be allocated
    if (ptr == NULL) {
        printf("Error! memory not allocated.");
        exit(0);
    }

    printf("Enter elements: ");
    for (int i = 0; i < n; ++i) {
        scanf("%d", ptr + i);
        sum = sum + *(ptr + i);
    }

    printf("Sum = %d", sum);

    // deallocating the memory
    free(ptr);

    return 0;
}
  • 1
    Definitely not useless, it's a pointer offset! Print the value of `ptr + i` before and after the `scanf` call... – bnaecker May 17 '20 at 18:32

5 Answers5

2

The 'i' isn't supposed to change the pointer. It is used to access the i'th element of the array that PTR points to. Without the I it would scan for the same element in this array, the first element

  • 1
    I would add that because he constructed an array of `int`s, `ptr+i` is used as (translated as) `ptr + "4bytes" * i` where `i` is the value of the `i` in the loop. The compiler knows that you are manipulating a pointer that points to `int`s (which is `ptr`) and knows how to translate this automatically – Ianau Andrei May 17 '20 at 18:40
1

'i' isn't a useless value, it is used to offset the pointer to access the correct element of the dynamic array.

It is the same as using

for (int i = 0; i < n; ++i) {
        scanf("%d", &ptr[i]);
        sum = sum + ptr[i];
    }
1

"What does int i do inside of the for loop?"

i is used for the pointer offset to access subsequent int objects of the dynamically allocated memory.

It works as:

(value of ptr [address of pointed object by ptr] + (i * sizeof(pointed object by ptr))).

Maybe take a look at this Stack Overflow question.

Note: The purpose is just to dereference and modify pointed to objects, not the pointer itself. ptr won´t get changed because of this pointer arithmetics.

"It doesn't change anything, even when I print &ptr it doesn't show me any difference in memory values."

Probably it doesn´t show different values because you print the address of the pointer itself, which doesn´t change by using offsets to the pointer.

I say "probably" because I don´t see how you actually print the address of ptr in particular. Maybe you even have some kind of kind of undefined behavior.

1

The purpose of the index i in the for is twofold:

  • ensure n numbers are read and cumulated in sum.
  • store each converted number into a separate entry in the array pointed to by ptr.

Given what the program does, it is not required to store the numbers into an array, or even necessary to allocate this array.

Here is a simpler version:

#include <stdio.h>

int main() {
    int n, v, sum = 0;

    printf("Enter number of elements: ");
    if (scanf("%d", &n) != 1)
        return 1;

    printf("Enter elements: ");
    for (int i = 0; i < n; i++) {
        if (scanf("%d", &v) != 1) {
            printf("input error\n");
            break;
        }
        sum = sum + v;
    }
    printf("Sum = %d\n", sum);
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
1

If you don't use 'i' in both sum and scanf, there won't be any effect on sum because it will take input in the first index of the dynamic memory and overwrite it. This 'i' is helping to store inputs in the dynamic memory.