Hi All,
Given a function like this:
void multiply_by_2( int& element ) { element *= 2; }
And an integer array:
int values[4] = {1,2,3,4};
I'd like to call the function using one of the array elements as the function argument.
Something like this:
multiply_by_2( &values[1] );
But that's invalid syntax for XC. How do I pass (by reference) an element in the array to the function 'multiply_by_2'?
Array Elements as Function Arguments in XC
-
- Member
- Posts: 8
- Joined: Thu Dec 13, 2012 3:19 am
-
- Respected Member
- Posts: 318
- Joined: Tue Dec 15, 2009 12:46 am
For reference arguments the address is implicitly passed, so you can just write:markseel wrote:Hi All,
Given a function like this:
void multiply_by_2( int& element ) { element *= 2; }
And an integer array:
int values[4] = {1,2,3,4};
I'd like to call the function using one of the array elements as the function argument.
Something like this:
multiply_by_2( &values[1] );
But that's invalid syntax for XC. How do I pass (by reference) an element in the array to the function 'multiply_by_2'?
Code: Select all
multiply_by_2(values[1]);
-
- Member
- Posts: 8
- Joined: Thu Dec 13, 2012 3:19 am
Makes sense - thanks for posting!
So given this function:
void multiply_3_elements_by_2( int values[] )
{
values[0] *= 2;
values[1] *= 2;
values[2] *= 2;
}
And an integer array:
int values[4] = {1,2,3,4};
How do I pass the array such that the last three elements are used?
I want to pass a reference/pointer to the array element existing at a non-zero offset.
In 'C' I'd do something like this:
multiply_3_elements_by_2( array + 1 );
... or ...
multiply_3_elements_by_2( &array[1] );
How do we do this in 'XC'?
So given this function:
void multiply_3_elements_by_2( int values[] )
{
values[0] *= 2;
values[1] *= 2;
values[2] *= 2;
}
And an integer array:
int values[4] = {1,2,3,4};
How do I pass the array such that the last three elements are used?
I want to pass a reference/pointer to the array element existing at a non-zero offset.
In 'C' I'd do something like this:
multiply_3_elements_by_2( array + 1 );
... or ...
multiply_3_elements_by_2( &array[1] );
How do we do this in 'XC'?
-
- Respected Member
- Posts: 318
- Joined: Tue Dec 15, 2009 12:46 am
This isn't possible in XC. Instead you have to pass the array and the offset to start at as separate arguments. Having a way to pass a slice of an array is something that would be useful and it is something we will consider when we next make changes to the the language.markseel wrote:How do I pass the array such that the last three elements are used?
I want to pass a reference/pointer to the array element existing at a non-zero offset.