in Programming in C
329 views
0 votes
0 votes

Suppose, I have this program…

#include <stdio.h>
#include <stdlib.h>

int *g(void)
{
    int *px;
    px = (int*) malloc (sizeof(int));
    *px = 10;
    return px;
}

int main()
{
   int y=*g(); /* Doubt */
   printf("%d",y);
}

Now, if I modify the line labelled as ‘Doubt’ as :- int y=g(), then in the output, I get a garbage value. I am unable to understand why. Can anyone please explain ?

in Programming in C
by
329 views

3 Comments

0
0
int *g()

$g$ is a function which returs a integer pointer

 

int y=*g();

Read it like this, $int \,\,y={}^*(g());$

Without *, we are giving a pointer to int, without casting so it  gives a garbage value.

1
1
Yeah I figured it out.. Thanks a lot. :)
0
0

Please log in or register to answer this question.

Related questions