Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

C errors

Status
Not open for further replies.

elMickotanko

New Member
Hi,

I've made a routine to take an average of the last ten values of a sensors.

Im getting a compile error that i cant understand

Code:
void takeaverage(signed int store[])
{
    
    signed int avg = ((store[0]+store[1]+store[2]+store[3]+store[4]+
    store[5]+store[6]+store[7])>>3);
    //use 8 values and shift right by three to divde by 8 - a lot quicker 
    than division.
    
    return (avg);
}

Ive tried return avg aswell. (without brackets)
its called from main like this:
Code:
middle = takeaverage(MIDstore); // get average for middle sensor

middle is declared as an int and MIDstor is declared as an array of ints at the start

the error is:

C1849: Result returned in void-result-function

I looked it up in codewarrior:
Code:
Description
A return was made with an expression, though the function 
as void return type. 

Example
void f(void) {
  return 1;
}

Tips
Do not return an expression in a function 
with void return type. Just write return, or write nothing.

Has any1 got any ideas? i cant see anything wrong with it?

PS. is there a better way to add up all values in an array rather than array1+array2...

cheers
 
Nigel Goodwin said:
Can't help you with C (never use it), but it doesn't look a good idea using shift to divide by eight when it's a signed variable.

aah good point, cheers. I'll make them unsigned and typecast them later when using them in my error variable.
 
That function should return a void type (nothing). If you want to return an integer value you have to write

Code:
[b]int[/b] takeaverage(signed int store[]) 
{ 

   //  code here

}
 
eng1 said:
That function should return a void type (nothing). If you want to return an integer value you have to write

Code:
[b]int[/b] takeaverage(signed int store[]) 
{ 

   //  code here

}

Brilliant, thanks! I was thinking the argument was wrong. I should have spotted that.
 
elMickotanko said:
Brilliant, thanks! I was thinking the argument was wrong. I should have spotted that.

Isn't it you are passing an array, or the address of the array to the takeaverage() function?

I would have use the following syntax:
Code:
signed int takeaverage(signed int* store) 
{ 

   //  code here

}
 
Last edited:
Yeah, i think the square brackets shouldn't be there. I originally didn't have them but tried putting them in to see if it worked. Im not sure about the ' * ' though. Why do I need that?
 
Since the array must have been declared as a global variable somewhere, I would use

Code:
unsigned int store[8];
char index;
unsigned int mval;
// ...
//------------------------------------------------------
void takeaverage()
{
    mval = 0;
    for (index=0; index<8; index++)
             mval += store[index];
    mval /= 8;
   
    return;
}
//------------------------------------------------------
After calling the function, the mean value is stored in mval and you can convert it to signed int (cast), if you want.
 
eblc1388 said:
Isn't it you are passing an array, or the address of the array to the takeaverage() function?

The following declarations are equivalent:
type1 foo(type2* vect);
type1 foo(type2[] vect);
type1 foo(type2[8] vect);

In any case you're passing the address of the first element. The size is not part of the argument, but you can pass it as an integer though.
 
eng1 said:
Code:
unsigned int store[8];
char index;
unsigned int mval;
// ...
//------------------------------------------------------
void takeaverage()
{
    mval = 0;
    for (index=0; index<8; index++)
             mval += store[index];
    mval /= 8;
   
    return;
}
//------------------------------------------------------

eng1, how can you be sure the total inside mval does not exceed its maximum limit befor division by 8?

For example, say unsigned int has a limit of 0-65535. What happens when we add eight numbers of 60000?
 
eblc1388 said:
eng1, how can you be sure the total inside mval does not exceed its maximum limit befor division by 8?

For example, say unsigned int has a limit of 0-65535. What happens when we add eight numbers of 60000?

I always evaluate the chance of an overflow. I was adding a comment before, but I didn't...
The values are coming from a sensor... assuming that they have 12-bit resolution or less, you have:
1023*8 = 8184 max with 10-bit resolution (typical for mcu ADCs)
4095*8 = 32760 max with 12-bit resolution
 
Last edited:
eblc1388 said:
eng1, how can you be sure the total inside mval does not exceed its maximum limit befor division by 8?

For example, say unsigned int has a limit of 0-65535. What happens when we add eight numbers of 60000?

my ADC is 10-bit so the values wont be higher than 1024.

eng1 said:
Since the array must have been declared as a global variable somewhere, I would use

Code:
unsigned int store[8];
char index;
unsigned int mval;
// ...
//------------------------------------------------------
void takeaverage()
{
    mval = 0;
    for (index=0; index<8; index++)
             mval += store[index];
    mval /= 8;
   
    return;
}
//------------------------------------------------------
After calling the function, the mean value is stored in mval and you can convert it to signed int (cast), if you want.

There is 5 sensors each with their own array. Thats why im passing parameters to it so the function can be used by all 5. store is just a local variable.
I'm using shift right rather than divide because it translates into less assembly and is quicker.

Thanks for your help guys.

I'll keep all the variables as unsigned int. My error variable which has to be signed will be something like this:

Code:
error = ((x*outside_leftsensr)+(y*inside_lftsnsr)-(y*ins_rightsen)
            -(x*out_rghtsnsr))
i think i should typecast them here to make sure the compiler doesnt get funny
 
ahmedragia21 said:
hi, what are you trying to do ^^ ?
im keen on sensors topics

Hi,

Its a line follower project for uni. We've got 5 phototransistor/led pairs at the front for detecting the line, and another two at the sides for detecting red and blue. There is a big electromagnet at the side to pick up a metal red or blue disc and drop it in the right bit.

Were using the MC9S08QG8 uC from freescale.

the sesnors are SFH300's. I really like them, the output has a very good response. When we first tried them we could see a rectified 100hz wave on the scope which was the room lighting. We've cut out the ambient light with heatshrink over sensors now !
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top