Hi ,
I am doing one sample program with function pointer.
Missing part for me is how bags array first index is getting replaced .
Line number 10 calls function next , but how come value getting changed in bags arrary.
Below is the code.
Code:
1#include <stdio.h>
2#include <stdlib.h>
3
4int bags[5]={20,5,20,3,20};
5
6main(void)
7{
8 int pos=15,*next();
9 printf("%d %d\n",bags[0],bags[1]);
10 *next()=pos;
11 printf("%d %d %d",pos,*next(),bags[0]);
12}
13
14
15
16int *next()
17{
18 int i;
19 for (i=0;i<5;i++)
20 if (bags[i]==20)
21 {
22 return (bags+i);
23 }
24 printf("Error!");
25 exit(0);
26}
output:
20 5
15 20 15
Any inputs on this would be highly appreciated.