views:

113

answers:

2

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 have a little knowledge in c++, someone just asked me.

Thanks!

+2  A: 

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.

AndreyT
I think what my guy wants me to do is to empty or null the array that is being passed.
jun
You cannot "empty" and array. C-style arrays are "non-emptyable". They have fixed size. Again "to null an array" would usually mean "fill it with zeroes". Finally, if you need to *deallocate* (destroy) the array, there's no way to do it without knowing how it was allocated (and it is nor clear what is the point of writing a dedicated function for it).
AndreyT
If it was dynamically allocated, then it wouldn't be an array of 25 ints anymore (which makes me wonder why one would use such an awkward way to pass something, to achieve such an "interesting" limitation).
UncleBens
+1  A: 

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.

dauphic