0
Why i cannot write this see the commented below in the following program?
#include <stdio.h> int pass(int*); int print(int*); int main() { int a[5]; pass(a); print(a); return 0; } int pass(int *p) { int i; for (i=0;i<5;i++) scanf("%d",&a[i]); // instead of scanf("%d",p+i); } int print(int *p) { int i; for (i=0;i<5;i++) printf ("%d",a[i]); // instead of printf("%d",*(p+i)); }
3 Answers
+ 2
'a' has a scope limited to main function. It cannot be accessed inside the pass or print method.
+ 1
Since you have passed the pointer(p) and not a, the function doesn't recognize what is 'a' , if you want to use 'a' , make the array public instead of declaring in the main() function.
0
Also, pass() and print() functions doesn't return any value , so change it to
void pass(int *);
void print (int *);
Similarly change in the definition also.