How to pass array to function?
-
Hi all: I have the following codes:
typedef struct Point3Struct { double x, y, z; } Point3; ... Point3 Object1_vertex[] = {...} ... void someFunction(Point3 *vertex_list){ size_t size = sizeof(vertex_list) / sizeof(Point3); ... } int main(){ someFunction(Object1_vertex); }
By passing the vertex array into the function "someFunction", I want to calculate the size of the array and do some other operations. But why this doesn't work? When I trace the program, the varialbe size is always 0. And the vertex_list is just the first element of the array. Other than that, if I try to do something like size_t size = sizeof(Object1_vertex) / sizeof(Point3); It will give me an error message saying that error C2070: 'Point3 []': illegal sizeof operand which complains about the "sizeof(Object1_vertex)". How should I solve the problem please? If I want to pass an array into a function, is this the correct way to do that???Asura
-
Hi all: I have the following codes:
typedef struct Point3Struct { double x, y, z; } Point3; ... Point3 Object1_vertex[] = {...} ... void someFunction(Point3 *vertex_list){ size_t size = sizeof(vertex_list) / sizeof(Point3); ... } int main(){ someFunction(Object1_vertex); }
By passing the vertex array into the function "someFunction", I want to calculate the size of the array and do some other operations. But why this doesn't work? When I trace the program, the varialbe size is always 0. And the vertex_list is just the first element of the array. Other than that, if I try to do something like size_t size = sizeof(Object1_vertex) / sizeof(Point3); It will give me an error message saying that error C2070: 'Point3 []': illegal sizeof operand which complains about the "sizeof(Object1_vertex)". How should I solve the problem please? If I want to pass an array into a function, is this the correct way to do that???Asura
You'll simply need to pass the size of the array as a separate argument. If you want to know more details on why this is happening, see this discussion[^], or search for "array decay" on the Internet.
-
Hi all: I have the following codes:
typedef struct Point3Struct { double x, y, z; } Point3; ... Point3 Object1_vertex[] = {...} ... void someFunction(Point3 *vertex_list){ size_t size = sizeof(vertex_list) / sizeof(Point3); ... } int main(){ someFunction(Object1_vertex); }
By passing the vertex array into the function "someFunction", I want to calculate the size of the array and do some other operations. But why this doesn't work? When I trace the program, the varialbe size is always 0. And the vertex_list is just the first element of the array. Other than that, if I try to do something like size_t size = sizeof(Object1_vertex) / sizeof(Point3); It will give me an error message saying that error C2070: 'Point3 []': illegal sizeof operand which complains about the "sizeof(Object1_vertex)". How should I solve the problem please? If I want to pass an array into a function, is this the correct way to do that???Asura
size_t size = sizeof(vertex_list) / sizeof(Point3); won't work since vertex_list is a pointer and its size is 4 (or 8). This... size_t size = sizeof(Object1_vertex) / sizeof(Point3); ...works for me (size == 3 if I initialize with 3 Point3 structs) but that makes passing the array useless since the array is accessible from anywhere in the same file anyway. You may need to pass the size along with the array pointer unless you can define the array with a set size. Mark
-
Hi all: I have the following codes:
typedef struct Point3Struct { double x, y, z; } Point3; ... Point3 Object1_vertex[] = {...} ... void someFunction(Point3 *vertex_list){ size_t size = sizeof(vertex_list) / sizeof(Point3); ... } int main(){ someFunction(Object1_vertex); }
By passing the vertex array into the function "someFunction", I want to calculate the size of the array and do some other operations. But why this doesn't work? When I trace the program, the varialbe size is always 0. And the vertex_list is just the first element of the array. Other than that, if I try to do something like size_t size = sizeof(Object1_vertex) / sizeof(Point3); It will give me an error message saying that error C2070: 'Point3 []': illegal sizeof operand which complains about the "sizeof(Object1_vertex)". How should I solve the problem please? If I want to pass an array into a function, is this the correct way to do that???Asura
In addition to the previous answer, I suggest that you take a look at the container classes from the STL (std::list or std::vector for example). At first sight it might be more complicated but it solves a lot of problems (and finally ease your life because you won't need to manage the memory yourself).
Cédric Moonen Software developer
Charting control [v1.1]