Why am I making a mistake adding two numbers using pointers?

Asked

Viewed 28 times

-1

#include <stdio.h>


int* adicionar(int *a,int *b)
{
    int c = *a + *b;
    return &c;
}



int main()
{
    int primeiro, segundo;
    int* temp;

    scanf("%d%d",&primeiro, &segundo);
    temp = adicionar(&primeiro, &segundo);

    printf("O resultado da soma é :%d",*t);
    return 0;
}

I’m trying to add two numbers using pointers, but it’s going wrong. What I’m doing wrong?

  • this program nor compiles...

1 answer

0


int* adicionar(const int *a, const int *b)
{
    static int c = *a + *b;
    return &c;
}

The pointer has an invalid value because you are returning a pointer with a local variable. So the pointed variable is not active after exiting the method.

Browser other questions tagged

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