Previous | Home | Next |
C programming language allows to return an array from a function. Similarly, C allows you to return a pointer from a function.
int * pointerFunction() { . . . }
Consider the following function which will generate 10 random numbers and return them using an array name which represents a pointer ie address of first array element.
#include <stdio.h> #include <time.h> /* function to generate and retrun random numbers. */ int * randomGenrate( ) { static int rdm[10]; int i; /* set the seed */ srand( (unsigned)time( NULL ) ); for ( i = 0; i < 10; ++i) { rdm[i] = rand(); printf("%d\n", rdm[i] ); } return rdm; } /* main function to call above defined function */ int main () { /* a pointer to an int */ int *p; int i; p = randomGenrate(); for ( i = 0; i < 10; i++ ) { printf("*(p + [%d]) : %d\n", i, *(p + i) ); } return 0; }
1092595084
571930317
1691085735
579972240
1561315090
1386912705
1983578941
958880236
2026599272
376002271
*(p + [0]) : 1092595084
*(p + [1]) : 571930317
*(p + [2]) : 1691085735
*(p + [3]) : 579972240
*(p + [4]) : 1561315090
*(p + [5]) : 1386912705
*(p + [6]) : 1983578941
*(p + [7]) : 958880236
*(p + [8]) : 2026599272
*(p + [9]) : 376002271
Previous | Home | Next |