Variables in array as an argument to function

Hey guys, been awake almost all night trying to find desperately a solution to my problem. But it seems like this one is too hard for me this time.

In my program I have an array which stores 3 variables; let's say array[1] = 2, array[2] = 4 and array[3] = 6. Now I need to send this array to a function and return one value from there to main program.

As far as I know, a function cannot directly have array as an argument. I have tried to learn to use pointers, but I don't understand them to be able to adapt the information, no matter how hard I try.

This is what I would like to get with bad-coding.

from main:
process_array(array[1], array[2], array[3]);

the function:
1
2
3
4
5
int process_array(int array[1], int array[2], int array[3]) {
    int new_value;
    new_value = array[1] + array[2] + array[3];
    return new_value;
}


Now value 12 should return to main program.

I would like also to know, if it is possible to use arrays without filling the place array[0]?

Thanks for your time!
Last edited on
Yes this is one page I have read. Sorry but it was not enough.

But I will read it once more.

EDIT:

Thank you very much matsom! In a first sight it looks like it really helped me to sort out things. Now I feel a little bit disappointed checking also that page without finding this solution by myself. I was just blind. That array-page was very informative. But the same I cannot say about all the pages I went through about arrays and functions.

But before I declare this topic as solved, I will check my code further with arrays.
Last edited on
pekde wrote:
I would like also to know, if it is possible to use arrays without filling the place array[0]?

Yes, but you have to be mindful of errors eg.
1
2
3
4
5
6
7
8
9
int iarray[3];
for(int i = 1; i < 3; ++i)
    iarray[i] = 1; //iarray[1] = 1, iarray[2] = 2
//somewhere else...
int sum = 0;
for(int i = 0; i < array_size; ++i)
    sum += iarray[i]; //iarray[0] is not initialized, may contain garbage number

std::cout << sum; //error! 


p/s the tutorial was meant as a supplement to your text books and lecture notes
Last edited on
Topic archived. No new replies allowed.