Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I need help passing an array between functions without it being global 1

Status
Not open for further replies.

soas

Technical User
Aug 3, 2001
49
US
Hey guys, I am a novice programmer, with no formal experience. I think this is common knowlege amoung programmers with formal training, but how do i pass an array between two functions?
I know I cant do it like this
Void blah(int x[10][10])
I am barely familiar with pointers, but not enough to know if thats what i need to use here, to pass the array between two functions without making it global. The thing is I wouldnt even ask if i had a clue what to look for to learn it myself, but i just dont know what to look for to find the answer. I would be very greatful if someone could help me with possible ways to do this. If you need me to explain better what I am trying to accomplish, I will try my best.
If it is pointers that I need to use, I should add that I only know the idea of pointers, not how to use them, so if someone could show an example or where to learn about them I would be equally happy with that. I did look up pointers, but apparently its a much broader subject than I anticipated. I will be greatful for any help at all, thanks!
 
You don't need to define size. You should write
Code:
void blah(int x[][])
or
Code:
void blah(int *x)
.
When you call the procedure, variable size should be reserved. A sample code could be:
Code:
int main(){
int x[10][10];

blah(x);
}
A rule about pointers you'll always have to take into account is that you must always have its size reserved before using it. For example if you wrote this:
Code:
int main(){
int *x;

blah(x);
}
it would compile, but it wouldn't be correct. It would usually crash.

Think on pointers as an arrow pointing at something be it an int, char, string or whatever you like.
 
woohoo, thank you! that part just confused me
I would have always considered x the variable to be totally different than x the array, the part that always threw me off was the blah(x), i would do blah(x[][])
Thanks Alot!
 
Use the following decleartion for passing Two Dimensional arrays ( its array of arrays ) to a function.

#include <iostream.h>
void show(int (*)[3]);
int main()
{
int a[3][3] ={10, 20, 30, 40, 50, 60, 70, 80, 90};
show(a);
return 0;
}

void show(int (*arg)[3])
{
int i, j;
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
cout << endl << arg[j];
}
Here the (*arg)[3] is a pointer to a 3 element array. While using like this you should know the row(number of arrays) size within the function otherwise pass it to the function.

Maniraja S
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top