2016-08-27 6 views
0

私のプログラムで数字の最上位ビットを検索するための関数を作成する際に問題があります。ここで私はそれをテストするために使用したコードは次のとおりです。関数の型エラーの競合 - C

#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 
} 

私はQtの創造主を使用していて、エラーがある:私が撮影した

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

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

そして、オンラインでソリューションを探すことはできますが、この関数呼び出しが失敗する原因となっている障害は確認できません。この関数を動作させるには、どのように構文を修正する必要がありますか?

void msbFinder(unsigned int); 

と機能を定義しながら、次のように定義されて - - 前方宣言の関数型で

答えて

2

宣言は言う:

void msbFinder(unsigned int); 

関数の定義は言う:

unsigned int msbFinder(unsigned int x) 

あなたはvoidunsigned intの違いを参照していますか?宣言は定義に一致する必要があります。

+0

Dammit、私は馬鹿です。どうもありがとうございました... –

2

voidであるあなたがunsigned intに前方宣言で関数の型を変更する必要が

unsigned int msbFinder(unsigned int x) /* <-- type given as unsigned int */ 

。ファイルの先頭に