array of pointers as function parameter
Both
int getResult(Foo** fooPtrArray)
and
int getResult(Foo* fooPtrArray[])
as well as
int getResult(Foo* fooPtrArray[4])
will work perfectly fine (they are all equivalent).
It is not clear from your question what was the problem. What "failed"?
When passing arrays like that it normally makes sense to pass the element count as well, since the trick with allowing the array type to decay to pointer type is normally used specifically to allow passing arrays of different sizes:
int getResult(Foo* fooPtrArray[], unsigned n);
...
Foo* array3[3];
Foo* array5[5];
getResult(array3, 3);
getResult(array5, 5);
But if you are always going to pass arrays of strictly 4 elements, it might be a better idea to use a differently-typed pointer as a parameter:
int getResult(Foo* (*fooPtrArray)[4])
In the latter case the function call will loook as follows
Foo* array[4];
getResult(&array);
(note the &
operator applied to the array object).
And, finally, since this question is tagged as C++, in the latter case a reference can also be used instead of a pointer
int getResult(Foo* (&fooPtrArray)[4]);
...
Foo* array[4];
getResult(array);
Passing an array containing pointers to a function properly
Try this:
void printArray(int **iXArray, int iSize) ...
In your example you provide an array of (int*) so reference it as one, you must tell the compiler to expect an array of pointers.
By default passing an array is by reference. If you change the array's content, it changes at the callee's side aswell. The pointer itself is passed by value, so changing the value of the iXArray parameter (iXArray = (int**)123;
) will not change the array2
pointer at the callee's side.
If you want to pass the array by value, will need to wrap it in a value type:
typedef struct {
int A[123];
} Array;
Array incrementArray(Array array, int count) {
for (int i=0; i<count; i++) {
array.A[i]++;
}
return array;
}
C pass int array pointer as parameter into a function
In your new code,
int func(int *B){
*B[0] = 5;
}
B
is a pointer to int
, thus B[0]
is an int
, and you can't dereference an int
. Just remove the *
,
int func(int *B){
B[0] = 5;
}
and it works.
In the initialisation
int B[10] = {NULL};
you are initialising anint
with a void*
(NULL
). Since there is a valid conversion from void*
to int
, that works, but it is not quite kosher, because the conversion is implementation defined, and usually indicates a mistake by the programmer, hence the compiler warns about it.
int B[10] = {0};
is the proper way to 0-initialise an int[10]
.
Related Topics
How to Load & Call a Vbscript Function from Within C++
Qscrollarea with Dynamically Changing Contents
Preferred Cmake Project Structure
How to Write a Large Number in C++ Source Code with Spaces to Make It More Readable
How to Not Wait for a System() Command to Finish? (In C)
Is It Ok to Specialize Std::Numeric_Limits<T> for User-Defined Number-Like Classes
Why "Not All Control Paths Return a Value" Is Warning and Not an Error
_Attribute_((Format(Printf, 1, 2))) for Msvc
Qdialog Exec() and Getting Result Value
Gprof Reports No Time Accumulated
Auto' as a Template Argument Placeholder for a Function Parameter
Why Is a Class Allowed to Have a Static Member of Itself, But Not a Non-Static Member