I'm not sure if I understood you correctly. But it seems what you need to know is how to pass arrays as arguments to functions and how to return arrays from functions and you are aso having doubts on how arrays are created and initialized.
Lets declare an array of ints and initialize it:
Code:
int myarray[5] = {11,12,13,14,15};
// or
int myarray[] = {11,12,13,14,15}; // dimensions are taken from the initialization
Arrays cannot be copied or assigned.
Code:
int anotherarray[] = myarray; // error. cannot assign one array to another.
int anotherarray[](myarray); // error. Cannot initialize an array as a copy of another.
So the "trick" (there's not really a trick. It's how you should do it), is to keep the array size as a const variable and to use for loops to initialize arrays from other arrays.
Code:
const int ARRAY_SIZE = 5;
int myarray[ARRAY_SIZE] = {11,12,13,14,15};
int anotherarray[ARRAY_SIZE];
for (int i = 0; i != ARRAY_SIZE; ++i) {
anotherarray[i] = myarray[i];
}
When passing arrays to functions the array_size becomes a fundamental aspect of array usage. How would a function know the array size without this very important piece of information be passed to it? It wouldn't. So, the size of the array has to make it's way onto the function parameters list.
Let's create a function that adds some value to each element of an array.
Code:
void add_to_elements(int* arr, const int size, int value) {
// arr is a pointer to int (read below)
// size is the size of the array passed
// value is the value to add to each element
for (int i = 0; i != size; ++i) {
arr[i] += value;
}
}
int* refers to a pointer to int. This is an important concept when passing arrays to functions. An array name is automatically converted to a pointer to the first element. As you will see below, you pass the array by simply using its name. Thus the function must be declared as expecting a pointer to the first element of the array. Since the array stores ints, your parameter must be a pointer to int. To call the function:
Code:
int someval = 12;
const int ARRAY_SIZE = 5;
int myarray[ARRAY_SIZE] = {11,12,13,14,15};
add_to_elements(myarray, ARRAY_SIZE, 5); // Ok.
add_to_elements(someval, ARRAY_SIZE, 5); // Error!
As you see the array is passed like any other variable. You just put its name. But contrary to other variables, when just named, an array is converted to a pointer to int. The second call is an error. someval is an int, not a pointer to int, thus this call gets immediatly flagged by the compiler.
The reason why I'm making a mention to it is because a nasty error can occur from using functions that operate on arrays with non arrays as arguments. Change the first line of the previous code to this:
Code:
int val = 12;
int* someval = &val;
someval is now a pointer to int. Your compiler will happily proceed without an error. But what have you done! When you run the program, the function will be called and this bit inside the function definition will wreak havoc:
Code:
for (int i = 0; i != size; ++i) {
arr[i] += value;
}
The program will crash and you won't know any better. For this reason some people prefer to declare their functions that operate on arrays only like this:
Code:
void add_to_elements(int arr[], const int size, int value)
It at least helps to remind that arr is an array. However it has the side-effect of making the less attentive think that they are passing an array to a function. They aren't. They are passing still a pointer to the first element. Technically you can't pass arrays to functions. Although we say you do, the reality is that you are passing a pointer to the first element.
So, now you already have a pretty good idea on how to pass arrays to functions. But how about returning arrays? You can't. You can however return a pointer to the first element of the array or the element itself.
Code:
int get_array_element(int* arr, int elem) {
return arr[elem];
}
int* get_pointer to_first_array_element(int* arr, const int ARRAY_SIZE) {
// do whatever you want to do to the array
return arr; // return the array name which is converted to a pointer to the first element.
}
Do note however that on the first function if elem is bigger than the array size you will be over the array boundaries, generating a nasty bug. So your code should check, prior to calling the function that elem is correct.
And I guess this pretty much covers what you want to do.