W
W
whatislov2021-06-11 11:53:13
C++ / C#
whatislov, 2021-06-11 11:53:13

How to pass a one-dimensional array from one function to another?

Good afternoon! How to pass a one-dimensional array from one function to another? I have an array forming function and an array printing function. How do I pass an array from one to the other in order to print it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wataru, 2021-06-11
@wataru

If this is C++, then use std::vector. One function creates and returns it, another prints it.
To avoid unnecessary copying, pass as a constant reference:

std::vector<int> MakeArray();
void Print(const std::vector<int> array);

If you need to work with arrays, then you will have to return the length separately through the output parameter. Pass as pointer + length.
int* MakeArray(int *len);
void Print(int *array, int len);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question