Sum with vector and pointer in C

Asked

Viewed 111 times

-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 ?

1 answer

3


The mistake is that you are using a ponteiro as if you a variável. The ponteiro stores an address. When you do this:

soma = soma + vetor[i];

You’re putting a valor instead of a endereço. If you want to add up the values using ponteiros then you need a place to store those values, put the endereço from that location on ponteiro and then use the ponteiro to access this endereço. You can do this by using a variable or by making dynamic memory allocation.

Using a variable

int vetor[7], i;
int auxiliar; /* variavel auxiliar para armazenar a soma */
int *soma = &auxiliar; /* Colocando o endereco de auxiliar no ponteiro */
*soma = 0; /* Colocando 0 no endereco que soma aponta, que eh a variavel auxiliar */

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]; /* Colocando a soma dentro do endereco onde soma aponta */
}

printf("Soma dos dígitos do RU: %d\n", *soma);

Using the memory allocation

int vetor[7], i;
int *soma = malloc(sizeof *soma); /* Alocando um espaco de memoria para guardar o conteudo */

if (soma == NULL) return 1; /* Verificando se a alocacao foi feita com sucesso */

*soma = 0; /* Colocando 0 no endereco que soma aponta */

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]; /* Colocando a soma dentro do endereco onde soma aponta */
}

printf("Soma dos dígitos do RU: %d\n", *soma);

free(soma); /* Liberando espaco alocado */

See your code working:

Using variable

Using malloc

Some complementary answers

Is there a problem assigning a value to a pointer?.

What is the difference between vector pointer and variable pointer?.

What is indirect?.

Doubt about C pointers.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.