Ok, this has been bugging me all day, so I thought I'd turn to ATHT to see if anyone knows the answer.  I had a test in my embedded programming class (and kicked butt if I do say so myself), except for one question.  It asked us to rewrite this function to return -1 if the parameter passed into the function was out of a certain range.  Ok, fine, easy right?  The catch is that in the main function that calls this function, the code looks like this.  Oh, and it's an elevator control program if anyone is wondering.
char * pDIPSwitch1 = (char *) 0x40000000; //Or some memory address, don't remember it exactly
char next;
next = Get_Next_Floor(pDIPSwitch1);
So the idea is to write Get_Next_Floor to return the first three bits of the dip switch (which indicates the floor) to next, unless the dip switch is set for a floor that doesn't exist, then return -1. Well the first part is easy, but how in the world do you return -1 to a char (which is considered unsigned, right?)? The only thing I could think of was to return -1 as an 8 bit binary 2's compliment and then rely on the main function to treat it as an 8 bit int when it checked for the -1 return value. We could assume that the main function did whatever kind of checking it needed to, but the function Get_Next_Floor had to return -1. Our professor was very specific about this.
So, my question is this, how in the world do you return -1 to a char variable? Is my way the only way to do it, or is there an easier way? Thanks for any help you guys have, I'm tearing my hair out trying to figure this one out. Oh, and it was kind of a pointless question since there was no floor 0, so the return value for incorrect input could have just been 0, but that obviously wasn't the point of the question...I guess.
			
			char * pDIPSwitch1 = (char *) 0x40000000; //Or some memory address, don't remember it exactly
char next;
next = Get_Next_Floor(pDIPSwitch1);
So the idea is to write Get_Next_Floor to return the first three bits of the dip switch (which indicates the floor) to next, unless the dip switch is set for a floor that doesn't exist, then return -1. Well the first part is easy, but how in the world do you return -1 to a char (which is considered unsigned, right?)? The only thing I could think of was to return -1 as an 8 bit binary 2's compliment and then rely on the main function to treat it as an 8 bit int when it checked for the -1 return value. We could assume that the main function did whatever kind of checking it needed to, but the function Get_Next_Floor had to return -1. Our professor was very specific about this.
So, my question is this, how in the world do you return -1 to a char variable? Is my way the only way to do it, or is there an easier way? Thanks for any help you guys have, I'm tearing my hair out trying to figure this one out. Oh, and it was kind of a pointless question since there was no floor 0, so the return value for incorrect input could have just been 0, but that obviously wasn't the point of the question...I guess.
 
				
		 
			 
 
		 
 
		 
 
		 
 
		
 Facebook
Facebook Twitter
Twitter