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
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
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.
Summary: "Do I get an array of 30 elements which contain NULL?" A lot of things don't add up here. First you declare a "struct map {...}", then have the line of code "Map *mymap". Where did Map come from?? ...
Summary: You really don't have to know anything about pointers to figure this out. Look at these two lines of code: char *password_ptr = &passowrd; password_ptr = getpass("Password: "); If line one, you assign...