Passing by reference (structure & function)

I created a function called “setToGrey” that returns no value and accepts a single parameter of type “Colour”. How would I set the red, green and blue channels all to 128, leaving the alpha unaffected passing by reference? Hope this makes sense! Thank you!

#include "stdafx.h"
#include <iostream>
using namespace std;

struct colour
{
int red;
int green;
int blue;
int alpha;
colour()
{
red = 0;
green = 0;
blue = 0;
alpha = 255;
}

};

int main(){
colour colour1;
return 0;
}

void settogrey(colour) {

}
Last edited on
1
2
3
4
5
6
7
8
9
void settogrey(colour& the_input_parameter)
{
    the_input_parameter.red = 128;
    the_input_parameter.blue = 128;
    the_input_parameter.green = 128;
} 

colour colour1; // create an object of type colour, named colour1
settogrey(colour1); // pass that object to the function 



Last edited on
Like this:
1
2
3
4
5
6
void settogrey(colour& col) 
{
   col.red = 128;
   col.green = 128;
   col.blue = 128;
} 
Topic archived. No new replies allowed.