开发者

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


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


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;
}


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);


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.


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


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜