我遇到的问题是找到一个函数,用于查找在我的程序中运行最重要的数字。这是我用来测试它的代码:
#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 Creator,错误是:
void value not ignored as it ought to be
y = msbFinder(x);
^
和
conflicting types for 'msbFinder'
unsigned int msbFinder(unsigned int x)
^
我已经在线查看了解决方案,但是我看不到导致此函数调用失败的错误。如何修复语法以使此功能正常工作?
答案 0 :(得分:2)
在前向声明中,函数类型为void
-
void msbFinder(unsigned int);
在定义函数时,它被定义为 -
unsigned int msbFinder(unsigned int x) /* <-- type given as unsigned int */
您需要将前向声明中的函数类型更改为unsigned int
。
答案 1 :(得分:2)
文件顶部的声明说:
void msbFinder(unsigned int);
函数定义说:
unsigned int msbFinder(unsigned int x)
您是否看到void
和unsigned int
之间的区别?声明需要与定义相匹配。