Click to See Complete Forum and Search --> : Passing Arguments To Functions
LindisfarneBabe
02-19-2001, 08:20 PM
In C, can I pass an array to a function in a similar way that I can pass a variable to a function?
I got a nasty feeling the answer's going to by, "sure you can, but you got'a use pointers to do it."
Thanks for any help,
Lindi. :confused:
Strike
02-19-2001, 09:13 PM
You can pass an array just fine, but it really does pass a pointer anyway I believe - meaning that the data in the array is subject to permanent change if you alter it within the function.
binaryDigit
02-19-2001, 09:43 PM
if you're going to use c then learn to use pointers. eventually you'll start to love them.
Stuka
02-20-2001, 01:31 AM
They're right - although, ironically, if you pass a struct with an array member, you will actually get a copy of the array - if you REALLY need to do that!
Sterling
02-20-2001, 08:50 AM
You also need to pass, somehow, the size of the array. An unsigned int is probably a good bet for this. ;) The other way is to have some kind of "terminator value" at the end of the array. NULL or '\0' are usual suspects, but anything that wouldn't show up in your actual array data is fine.
Of course, if you're using C++ and the STL, you can just pass in a vector or something and avoid this problem entirely.
Energon
02-20-2001, 01:09 PM
Originally posted by Sterling:
You also need to pass, somehow, the size of the array. An unsigned int is probably a good bet for this. ;) The other way is to have some kind of "terminator value" at the end of the array. NULL or '\0' are usual suspects, but anything that wouldn't show up in your actual array data is fine.
Of course, if you're using C++ and the STL, you can just pass in a vector or something and avoid this problem entirely.
wouldn't the sizeof() the array divided by the sizeof() the array type give you the length of the array? Like:
int some_ints[25];
int array_size = sizeof(some_ints)/sizeof(int);
and then array_size would be 25... but I could be wrong...
[ 20 February 2001: Message edited by: Energon ]
f'lar
02-20-2001, 02:55 PM
No, it would return either the size of the pointer (4 bytes, I think). It might return the size of the array(I don't think, so but I'd have to try it to be certain), but it wouldn't let you know how many elements are in it. Anyway, you don't necessarily have to pass the size if you use it in a certain way, ie this:
void Somefunction(int array[])
{
//do something
}
is just fine.
You do have to be careful how you use it, though, because it won't always stop you from accessing memory past the end of the array.
[ 20 February 2001: Message edited by: f'lar ]
Energon
02-20-2001, 04:29 PM
*scratches head*
huh... are you sure? I'm going to have to test this because it seems to me that if sizeof(int) is say, 4, and sizeof(some_ints) is 4*25 (100) then 100/4 would give you 25, the size of the array... I can see why it might give you the sizeof(some_ints) as 4 because of passing by reference, but it doesn't seem like it would end up that way in this case for some reason...
can anyone shed some light?
Energon
02-20-2001, 04:38 PM
huh... tested it and you're right... if you pass it as a parameter to a function, you can't use that method to determine it's size... if you pass it to a function you end up w/ sizeof(int) is 4, sizeof(array) is 4, and sizeof(array)/sizeof(int) is 1... and you can't get around that...
good info to know...
Strike
02-20-2001, 04:39 PM
Energon, when you ask for sizeof(some_ints), you are asking for a sizeof(int *) which will just be the size of the pointer, which in this case is just 4.
Energon
02-20-2001, 04:54 PM
Originally posted by Strike:
Energon, when you ask for sizeof(some_ints), you are asking for a sizeof(int *) which will just be the size of the pointer, which in this case is just 4.
is there any way to ask for sizeof(*some_ints), (which would be the size of the array itself)?
Strike
02-20-2001, 07:22 PM
Not that I know of. That's why data structures are more commonly used, they can have members that keep track of their size and whatnot.
STL classes all have size() functions, I believe. So a vector could easily tell its length with a simple size().
pinoy
02-21-2001, 01:04 AM
arrays and pointers are very related. Sometimes I don't even know what the exact differences are. When used an argument to a function, arrays are basically treated as pointers, but when used alone, arrays have a size equal to the amount of memory it uses.
e.g.
int x[30];
size_t x_size = sizeof(x)/sizeof(x[0]);
printf("number of elements: %d\n", x_size);
the above code is going to return 30.
int *x = (int*)malloc(sizeof(int) * 30);
size_t x_size = sizeof(x)/sizeof(x[0]);
printf("number of elements: %d\n", x_size);
is going to return 1. Because sizeof(x), if x is a pointer returns how ever big a pointer size is in that system, usually 4 bytes. For an array it's the number of bytes the actual array takes.
Sterling
02-21-2001, 11:27 AM
Arrays and pointers are, IIRC, one and the same. At least with most modern C compilers, I believe there's no real difference. Indexing is translated to pointer arithmetic (no need to use that error-prone and ugly method now!), passing arrays means you pass a pointer, etc. I think the only real difference is that arrays have some special behavior for convenience sake. The previously-mentioned sizeof() example is one, and I think there's a couple of others.
Another note: gcc lets you allocate variable-size arrays. So you can do something like:
int foo = bar * garply; // Assume bar and garply are read from a file or something.
int baz[foo]; // Note that foo is not a constant!
Which means you don't really have to use malloc() and free() (or new and delete) for that anymore.