How to invoke C++ function?
I have a function with the prototype as
void test( int array [] , int b);
I know we can replce the protoype as: void test(int*, int);
In main()
we declare the following arrays :
int ar开发者_StackOverflow社区ray1[10], array2[10];
To set the body of the function to 0,
test ( array1 , b)
{
for ( int i = 0 ; i < b ; i++)
array1[i] = 0;
}
can i do the follwing and why?
int main()
{// assuming b is the size of the array
test(array1 , b);
test(array2 , b) ;
return 0;
}
i know the basic of c++ im trying to write my own include files. I am just wondering if this is possible and is it a good choise?
Not a direct answer to your question, but the fact that you talk about C++ and yet use plain old C arrays caught my attention:
Consider not using C arrays in the first place. Instead, use a std::vector<int>
. This probably avoids the need to ask this question in the first place (and it avoids a whole lot of other issues). You don't need to bother about the right size type (int
? size_t
? Something else?) since std::vector
gives you the right type already: std::vector<int>::size_type
.
Your function signature would just be
void test( std::vector<int> &a );
The implementation for filling the vector with zeros would be:
void test( std::vector<int> &a )
{
std::fill( a.begin(), a.end(), 0 );
}
You may be asking about the difference between formal parameters and actual parameters.
In your prototype
void test(int *array, size_t size);
the names 'array' and 'size' are the formal parameters. You use those names inside the body of your function.
In the code that invokes the function, you can use different names, which are the actual parameters.
so
int main()
{
const size_t b = 10;
int array1[10], array2[10];
test(array1 , b);
test(array2 , b) ;
return 0;
}
Here array1
and b
are the actual parameters to the first invocation and array2
and b
are the actual parameters to the second invocation.
So yes, you can use whatever names you like as actual parameters, so long as the types of the variables match your prototype.
Yes, it's possible; but declaration in the function body should be same as what you declared as prototype:
void test (int array1[], int b) // <---- see here (prefer `unsigned int` for size)
{
for ( int i = 0 ; i < b ; i++)
array1[i] = 0;
}
It's better to use library function memset() if you want to set something to 0
.
(As an advise, you can a build a library on top of what is already existing. Otherwise it will be like reinventing a wheel.)
Looks like you're migrating from C. Yes, this is possible, but you need to get the declarations right, or the compiler will throw an error.
The preferred C++ prototype would be
void test(int *array, size_t size);
In C++, you must declare the return type, and the type(s) of each argument in both the prototype and the implementation.
Note:
You don't need to use size_t
, but it is preferred (even on C). size_t
is included in stddef.h
(and by extension cstddef
which is the preferred C++ include). It is architecture dependent and is usually unsigned int in 32-bit systems and unsigned long long on 64-bit systems
精彩评论