Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass on value to an integer pointer

Tags:

c

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

like image 885
Registered User Avatar asked Mar 26 '11 11:03

Registered User


4 Answers

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

like image 127
Sandro Munda Avatar answered Oct 10 '22 13:10

Sandro Munda


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;
}
like image 41
David Heffernan Avatar answered Oct 10 '22 15:10

David Heffernan


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);
like image 30
Armen Tsirunyan Avatar answered Oct 10 '22 15:10

Armen Tsirunyan


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.

like image 2
aroth Avatar answered Oct 10 '22 14:10

aroth