MFN-Armsman MFN-Armsman - 1 year ago 82
C Question

Conflicting type errors in function - C

I'm having issues getting a function built for finding a numbers most significant bit operating in my program. Here's the code I was using for testing it:

#include <stdio.h>

void msbFinder(unsigned int);

int main()
unsigned int x;
printf("Input hexadecimal: ");
scanf("%x", x);
unsigned int y;
y = msbFinder(x);
printf("Most significant bit: %x", y);

unsigned int msbFinder(unsigned int x) //--Finds most significant bit in unsigned integer
unsigned int a; //--Declare int that will be manipulated
a = x; //--Initialise equal to passed value
a = a|a>>1;
a = a|a>>2;
a = a|a>>4;//--var is manipulated using shifts and &'s so every value at and beneath the MSB is set to 1
a = a|a>>8;//--This function assumes we are using a 32 bit number for this manipulation
a = a|a>>16;
a = a & ((~a >> 1)^0x80000000);//--Invert the int, shift it right once, & it with the uninverted/unshifted value
return (a);//--This leaves us with a mask that only has the MSB of our original passed value set to 1

I'm using the Qt Creator, and the errors are:

void value not ignored as it ought to be
y = msbFinder(x);


conflicting types for 'msbFinder'
unsigned int msbFinder(unsigned int x)

I've taken a look for solutions online, but I can't see the faults that are causing this function call to fail. How do I need to fix up my syntax to get this function to work?

Answer Source

The declaration at the top of the file says:

void msbFinder(unsigned int);

The function definition says:

unsigned int msbFinder(unsigned int x)

Do you see the difference between void and unsigned int? The declaration needs to match the definition.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download