Why i cannot write this see the commented below in the following program? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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)); }

20th Mar 2020, 12:38 PM
Yash Chaudhari
Yash Chaudhari - avatar
3 Answers
+ 2
'a' has a scope limited to main function. It cannot be accessed inside the pass or print method.
20th Mar 2020, 1:10 PM
Avinesh
Avinesh - avatar
+ 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.
20th Mar 2020, 1:11 PM
AMISHA NARSINGANI
AMISHA NARSINGANI - avatar
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.
20th Mar 2020, 1:13 PM
AMISHA NARSINGANI
AMISHA NARSINGANI - avatar