0

Passing Arrays into function. How do i fix the code?

#include <stdio.h> void display(int *); void show(int *); int main() { int num[]={1,2,3,4,5}, s; for(s=0; s<5; s++){ display(&num[s]); } return 0; } void display(int *x) { show(&x); } void show(int *a) { printf("%d\n", *a); //this is not printing the value. } I've tried using **a but the compiler doesn't execute, it throws a warning of incompatible pointer type.

31st Dec 2016, 10:02 AM
Maha Khan
Maha Khan - avatar
8 ответов
+ 1
Write in line 3 void show(int **); and in line 17 void show( int **a) The problem is as I mentioned before that x holds an address and show expects an address not the address to an address which is what you wanted to do by &x
31st Dec 2016, 11:07 AM
Andreas K
Andreas K - avatar
0
write show(x) instead of write(&x)
31st Dec 2016, 10:49 AM
Andreas K
Andreas K - avatar
0
did it not work?
31st Dec 2016, 10:53 AM
Andreas K
Andreas K - avatar
0
@AKC actually the book that I'm learning from requires me to write only the show function. It had show(&x) and then said to write a show function which prints all the values.
31st Dec 2016, 10:54 AM
Maha Khan
Maha Khan - avatar
0
ok that's a little bit confusing for me because x should be the the address not the value.
31st Dec 2016, 10:56 AM
Andreas K
Andreas K - avatar
0
I know!
31st Dec 2016, 10:59 AM
Maha Khan
Maha Khan - avatar
0
So I tried it without the "&" Then it works... But I try to figure out what the problem is when I dont modify at this position
31st Dec 2016, 11:01 AM
Andreas K
Andreas K - avatar
0
And that worked! Thank you!
31st Dec 2016, 11:08 AM
Maha Khan
Maha Khan - avatar