Passing by reference an array that has variables as elements

Hello everyone,

So I have 6 variables a,b,c,d,e and f. They are all integers. Now I have a function that wants to change the values of these variables. I don't want to pass by reference all these variables because (let's say) it doesn't look nice. So I thought that I could create an integer array with all these variables as elements. I managed to just create the array and use it inside a function but I don't know how to change the values through the function(pass by reference(?)). I read something about dynamic memory allocation but I don't know how to use it to pass by reference an array that contains variables. Thanks in advance
1
2
3
4
5

void function(int (&array)[size])
{
// do stuff
}
Last edited on

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void incr(int* vals[6])
{
	*vals[0] = *vals[0] + 1;
	*vals[1] = *vals[1] + 1;
}

int test()
{
	int a, b,c ,d, e, f;

	int* arr[6] = { &a, &b, &c, &d, &e, &f };

	incr(arr);
}



i think the array of pointers looks way more messy than just using variables, 6 parameters is nothing! many OS calls can take far more than that.

Golden Lizards solution will not work, it will only modify the values in the array, not the variables you loaded into it.
Last edited on
Hey,

First of all, thanks for the reply. I have two questions:
1. What is the function incr(int* vals[6]) doing?
2. Is int* x; the same as int *x;?

Cheers
1. incr() is adding 1 to each variable in the array (well, the first 2 because i'm lazy). its my example of a method that modifies several variables stored in an array. Of course you cant store a variable in an array, you can only store values in there.

so, you have 3 choices;
1. copy the variable values into an array, pass the array for modification by the method, copy the new values back out of the array into your variables.
2. populate the array with the addresses of your variables (pointers) so the method can access them indirectly (what i did)
3. pass the variables directly, by reference. which is what you should really do.


2. yes, its personal taste, i always use int* x; because i'm thinking "int pointer" others use int *x; because they want to associate (in their mind) the "pointerness" with x;

consider...

int a,*b,c; two ints and a pointer to an int. I can't do that with my style, but thats ok because i always declare each variable separately.
1
2
3
int a;
int* b;
int c;

it's just personal taste, or if you're a professional your employer may specify how it should be done in a "coding standard" document so all devs in a team do the same thing.
Last edited on
Thanks, much appreciated. I wanted to try this because some variables have long names and just the declaration of a function takes more than 1 line in the IDE. I guess I will have to make the names smaller :D
some variables have long names and just the declaration of a function takes more than 1 line in the IDE

Having meaningful names is a good thing, be careful not to sacrifice readability by using names which are hard to understand.

In the context of passing things to and from a function, sometimes it may be appropriate to use a structure as a way to pass multiple items as a single parameter, either by value or by reference as needed. Whether that makes sense depends on the circumstances.
Last edited on
if its just names that are your issue, then the problem is so much easier to fix :)

1
2
3
4
5
6
7
void myCoolFunctionThatDoesTheBestJob ( int firstReallyLongParametername
                                      , int secondReallyLongParametername
                                      , int thirdReallyLongParametername
                                      , int fourthReallyLongParametername
                                      , int fifthReallyLongParametername
                                      , int sixthReallyLongParametername);


remember, spaces, tabs and line feeds are meaningless to the compiler and it skips them when it finds more than one in a row, use that to your advantage and layout your source so its easy to read.

Where i work, some projects even align all declarations...
1
2
3
int              myInt   = 0;
float            myFloat = 0.0f;
std::vector<int> myVec   = {1,2,3};
Last edited on
Topic archived. No new replies allowed.