-5
Goal: Sum 7 numbers typed by the user that will be stored in an array, the sum must be done with a pointer.
Problem: The pointer generates an wrong result, if I take the asterisk from the pointer variable (which makes it a normal variable) the result is correct, but the variable as pointer is not the right one.
Example: The executable will ask for 7 digits, if in the 7 fields I type the number 1 the sum should be 7 but instead the sum given by the program is 28.
#include<stdlib.h>
#include<stdio.h>
#include<locale.h>
int main() {
setlocale(LC_ALL, "Portuguese");
int vetor[7], i;
int *soma;
soma = 0;
printf("Digite dígito por dígito do seu RU :\n");
for (i = 0; i < 7; i++)
{
printf("\n%dº dígito: ", i + 1);
scanf_s("%d", &vetor[i]);
soma = soma + vetor[i];
}
printf("Soma dos dígitos do RU: %d\n", soma);
system("pause");
return (0);
}
Attempts made: I tried to point the vector at the pointer this way: soma = &vetor[0];
(outside the for) and soma = &vetor[0 + i];
(within the for), but left only the most mistaken result.
How do I make this sum work using a pointer ?