C++ passing arrays to a method problem
Hey there I have this function:
void vdCleanTheArray(int (*Gen_Clean)[25])
{
}
I wanted to know what kind of array does it accepts and clear it.
Sorry, I 开发者_StackOverflowhave a little knowledge in c++, someone just asked me.
Thanks!
It accepts an array declared as
int array[25];
which should be passed "by pointer", i.e. by applying the &
operator to the array
vdCleanTheArray(&array);
As for how to clean it... As I understand, it is the above function that should clean it and you are supposed to write it, right? Well, inside the function you access the array by using the dereference operator *
as in
(*Gen_Clean)[i]
and you just "clear" it as you would clear any other array. Depends on what you mean by "clear". Fill with zeros? If so, then just write a cycle from 0 to 24 (or to sizeof *Gen_Clean / sizeof **Gen_Clean
) that would set each element to zero.
void vdCleanTheArray(int (*Gen_Clean)[25]) can be described as 'a function taking a pointer to an array of 25 integers and returning nothing.'
Assuming by 'clear' you mean set all values to 0, you could do it with something like this:
for (int i = 0; i < 25; i++) {
(*Gen_Clean)[i] = 0;
}
Note that in C++, C-style arrays cannot be resized. It's better to use std::vector if you need a resizable array, or std::array if you need a fixed-size array.
If you're new to C++, you probably shouldn't be around a function like this. The way the first parameter is typed isn't something you'll regularly (or should ever) see in C++.
To those wondering about the 'int (*Gen_Clean)[25]' syntax, I believe it's typed like this to enforce that the pointer being passed actually points to an array of 25 ints, as normally C++ would degrade a parameter 'int *Gen_Clean[25]' into a pointer-to-a-pointer, rather than a pointer-to-an-array.
精彩评论