Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Save to My Library
Look up keyword
Like this
1Activity
0 of .
Results for:
No results containing your search query
P. 1
c Return Pointer From Functions

c Return Pointer From Functions

Ratings: (0)|Views: 7 |Likes:
Published by Rakesh Patel

More info:

Published by: Rakesh Patel on Aug 14, 2013
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

08/28/2014

pdf

text

original

 
http://www.tutorialspoint.com/cprogramming/c_return_pointer_from_functions.htm
Copyright © tutorialspoint.com
RETURN POINTER FROM FUNCTIONS IN C
As we have seen in last chapter how C programming language allows to return an array from a function, similar way Callows you to return a pointer from a function. To do so, you would have to declare a function returning a pointer as inthe following example:
int *myFunction() {...}
Second point to remember is that, it is not good idea to return the address of a local variable to outside of the functionso you would have to define the local variable as
static
variable.Now consider the following function which will generate 10 random numbers and return them using an array namewhich represents a pointer ie address of first array element.
#include <stdio.h>#include <time.h> /* function to generate and retrun random numbers. */int *getRandom( ) { static intr[10];  inti;  /* set the seed */srand( (unsigned)time(NULL) );  for (i= 0;i< 10; ++i)  {r[i] =rand(); printf("%d\n",r[i] );  } returnr;} /* main function to call above defined function */intmain(){ /* a pointer to an int */ int *p;  inti; p=getRandom();  for (i= 0;i< 10;i++ )  {printf("*(p + [%d]) : %d\n",i, *(p+i) );  } return 0; }
When the above code is compiled together and executed, it produces result something as follows:
1523198053118721410711083009784304949591421301276

You're Reading a Free Preview

Download
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->