By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,231 Members | 1,634 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,231 IT Pros & Developers. It's quick & easy.

doubt regarding return value of a function

P: n/a
Hi,

I am not understanding this particular implementation:

/* example.c */
#include <stdio.h>

int gl;

int *fun ( void )
{
return &gl;
}

int main ( void )
{
int n;
*fun()=5; /* is this allowed ? */

n=*fun(); /* Not understanding how this is happening !! */
printf("%d\n",n); /* The value is printed here. Tried on VC compiler
*/

return 0;
}

Mar 16 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
felix wrote:
Hi,

I am not understanding this particular implementation:

/* example.c */
#include <stdio.h>

int gl;

int *fun ( void )
{
return &gl;
}

int main ( void )
{
int n;
*fun()=5; /* is this allowed ? */
Yes, fun() returns a pointer to int.
n=*fun(); /* Not understanding how this is happening !! */
Dereferencing the fun(), which is a pointer to int.

--
Ian Collins.
Mar 16 '07 #2

P: n/a
*fun()=5; /* is this allowed ? */
>
Yes, fun() returns a pointer to int.
n=*fun(); /* Not understanding how this is happening !! */

Dereferencing the fun(), which is a pointer to int.
I am still finding it difficult to understand. Please, can you tell a
bit more about the points that are made ?

--
Felix

Mar 16 '07 #3

P: n/a
felix wrote:
>> *fun()=5; /* is this allowed ? */

Yes, fun() returns a pointer to int.

>> n=*fun(); /* Not understanding how this is happening !! */

Dereferencing the fun(), which is a pointer to int.


I am still finding it difficult to understand. Please, can you tell a
bit more about the points that are made ?
Consider

int *p = fun()
int n = *p;

--
Ian Collins.
Mar 16 '07 #4

P: n/a
felix wrote:
I am not understanding this particular implementation:

/* example.c */
#include <stdio.h>

int gl;

int *fun ( void )
{
return &gl;
}
`fun` returns a pointer-to-int. Specifically, it returns
a pointer to the variable `gl`.
int main ( void )
{
int n;
*fun()=5; /* is this allowed ? */
Yes. If `E` is an expression of type pointer-to-T, then `*E` is
an lvalue (assignable-to expression [1]) and you can assign to
it if `E` is a legal pointer value. Which it is here, since it's
the address of `gl`.
n=*fun(); /* Not understanding how this is happening !! */
Similarly this gets the value of whatever integer variable [2]
the pointer returned by `fun()` points to.
printf("%d\n",n); /* The value is printed here. Tried on VC compiler
*/

return 0;
}
[1] Ignoring the small print for now.

[2] integer /object/, to be technical.

--
Chris "electric hedgehog" Dollin
"Our future looks secure, but it's all out of our hands"
- Magenta, /Man and Machine/

Mar 16 '07 #5

P: n/a
Got that,

Thank You Chris.
Thank You Ian.

Mar 20 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.