Help with pointers

-1

Can anyone help me with this code? it is not working, I can not find the error.

#include <stdio.h>

int chamar(int *n){

        printf("Digite o valor de n:");
        scanf("%d",&n);
        printf("%d",n);
        return 0;

        }

int main(void){

        int n;
        chamar(n);
        printf("\n%d\n",n);

        }
    
asked by anonymous 13.04.2016 / 04:31

1 answer

1

To insert the value in the variable n into main , you must pass the memory address of it, and you do this in scanf :

chamar(&n);

In the method call, since the parameter is already a pointer, you do not need to pass the memory address of it, thus getting:

int chamar(int *n){
    printf("Digite o valor de n:");
    scanf("%d",n);
    printf("%d",*n);

    return 0;
}

It can be seen that% with% of% with% with% has n , printf is used to create a pointer and to get the value from inside the pointer, and chamar serves to get the memory address of the variable.

    
13.04.2016 / 06:10