7
#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).

6 Answers 6

8

The solution of David Heffernan is true. However, if you want to use a pointer (to learn how a pointer works), you must allocate manually the variable with "malloc".

#include <stdio.h>
#include <stdlib.h> /* for malloc and free */

int main (void)
{
    int *ip = malloc(sizeof(int)); /* declare and allocate a new int pointer */
    printf("Enter a no \n");
    scanf("%d", ip);
    printf("ip = %d", *ip);

    free(ip); /* free the memory */

    return 0;
}

malloc() and free() is located in stdlib.h. When you declare :

int *ip;

You just allocate a pointer that points to "nothing". So you must allocate the necessary memory with malloc(). After that, *ip points to your new memory place and ip is the address of the new memory place.

You must free the memory manually when you no longer need the variable with free().

Sign up to request clarification or add additional context in comments.

1 Comment

yes you got it right I am trying to understand how pointer works.
6

You haven't allocated any storage, only a pointer. The pointer needs something to point at. You should do it like this:

#include <stdio.h>
int main(void)
{
    int i;

    printf("Enter a no \n");
    scanf("%d", &i);
    printf("i=%d", i);
    return 0;
}

I also took the liberty of making the declaration of main() valid, and returning a value.

If you want to learn about pointers, then try like this:

#include <stdio.h>
int main(void)
{
    int i;
    int* ip;

    ip = &i;
    printf("Enter a no \n");
    scanf("%d", ip);
    printf("*ip=%d", *ip);
    return 0;
}

Or if you want heap allocation rather than stack allocation:

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
    int* ip;

    ip = malloc(sizeof(int));
    printf("Enter a no \n");
    scanf("%d", ip);
    printf("*ip=%d", *ip);
    free(ip);
    return 0;
}

1 Comment

Hefferman no no I am not trying to test it the way you mentioned.I am just trying tp understand how can I store some thing when I declare some thing as a pointer this is just a small program which I am trying to work out.
2

you should allocate memory.

int* ip = malloc(sizeof(int));
scanf("%d", ip);
... 
free(ip)

Or, just pass the address of a stack variable

int i; 
scanf(%d, &i);

Comments

2

Here's one way to fix it:

#include<stdio.h>
int main () {
    int i;
    printf("Enter a no \n");
    scanf("%d", &i);
    printf("i=%d", i);
}

And another way is:

#include<stdio.h>
int main () {
    int* ip = malloc(sizeof int);
    printf("Enter a no \n");
    scanf("%d", ip);
    printf("ip=%d", *ip);
}

The problem with the original is that int* ip; is just declaring a pointer to an integer, but not allocating any memory to hold the actual integer, or setting the pointer to point to anything meaningful. You are just getting a pointer to some random memory location that may or may not be addressable by your program. So you get a segmentation fault when scanf() attempts to dereference this pointer to write data to it.

Comments

1

You need to assign memory to that pointer.

int *ip = malloc(sizeof(int));
// do stuff with ip

free(ip); //free the memory allocated

More : http://cslibrary.stanford.edu/102/PointersAndMemory.pdf

1 Comment

@N 1.1 that is a great link thanks for posting the link here.
1

No, you're wrong, ip is not an integer pointer. The type of ip is integer pointer, i.e. what you assign to ip should be an integer pointer. But you did not assign anything to ip. So where do you expect that scanf stores the integer value?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.