Computing.Net > Forums > Programming > question regarding an array

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

question regarding an array

Reply to Message Icon

Name: Justin
Date: May 9, 2003 at 13:27:49 Pacific
OS: WindowsXP
CPU/Ram: pentium4 (wish i had amd)
Comment:

Is it possible to pass an entire array by a functions argument without having to list the entire set .... ie (x[1],x[2],x[3]...)???


If so, is it even more so possible to pass by reference?

Thanx,
justin


PS: I'm using Dev 4 C++ and know this is capable under other languages, dont in in True Basic, but c++ is a tad bit different



Sponsored Link
Ads by Google

Response Number 1
Name: Richard
Date: May 9, 2003 at 16:20:01 Pacific
Reply:

I use visual C++ myself and usually use the CArray template object for setting up arrays. However your question relates more to C and passing simple arrays to functions.
The functions
void foo( int X[] );
void foo( int *X );
are equivalent.
Both pass the address of array X to the function.
The console program
main(int argc; char* argv[])
{
int x[10];
foo(x);
printf("foo is %d, %d, %d", x[0], x[1], x[2]);
getchar();
return 0;
}
foo( int X[])
{
X[0] = 1;
X[1] = 3;
X[2] = 5;
}

will print "foo is 1,3,5"

Please note that array the index always starts at 0;
You must keep track of the length of the array which is passed. Either it is known or you pass a length argument to the function; If you try to write data past the end of the array you will create a difficult to find bug. Thats why I should examine your template library for an array class.


0

Response Number 2
Name: JB
Date: May 13, 2003 at 22:38:14 Pacific
Reply:

Thankyou much that answers question ;]


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Dos Based GUI Libraries f... back-up batch for network...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: question regarding an array

Intitializing an Array in C www.computing.net/answers/programming/intitializing-an-array-in-c/8192.html

Pointing an array to an pointer! www.computing.net/answers/programming/pointing-an-array-to-an-pointer/8174.html

C question about char arrays www.computing.net/answers/programming/c-question-about-char-arrays/6100.html