mercredi 27 juillet 2016

how to pass on value to an integer pointer

#include<stdio.h>

int main ()
{
    int *ip;
    printf("Enter a no n");
    scanf("%d",ip);
    printf("ip=%d",*ip);
}

Above is my program and when I run it following is output

./a.out 
Enter a no 
10
Segmentation fault

I checked in gdb line 6 is problem which is

scanf("%d",ip);

So I infer that ip is a pointer to integer and %d expects an address value. While I am trying to pass on an integer to the value specified by ip so I think that is wrong. But what is the correct thing to do in the above situation. I want to use scan and assign an integer value to the address pointed to by ip (which in my case is missing).

Aucun commentaire:

Enregistrer un commentaire