【发布时间】:2021-09-23 02:53:36
【问题描述】:
当我尝试编译代码时,出现如下错误
main.c:12:15:错误:“ascii_to_hex”的类型冲突 unsigned char ascii_to_hex(unsigned char* buf)
#include <stdio.h>
#include <stdlib.h>
int main()
{
unsigned char str[] = {0x32, 0x35, 0x34, 0x035};
ascii_to_hex(str);
return 0;
}
unsigned char ascii_to_hex(unsigned char* buf)
{
unsigned char hundred, ten, unit, value;
hundred = (*buf-0x30)*100;
ten = (*(buf + 1)-0x30)*10;
unit = *(buf+2)-0x30;
value = (hundred + ten + unit);
printf("\nValue: %#04x \n", value);
return value;
}
我在这里做错了什么?
【问题讨论】:
-
str是unsigned char[4],但ascii_to_hex需要unsigned char*。改为通过&str[0] -
该错误应该更多,告诉您
ascii_to_hex函数有一个隐式声明。将函数移到main之上或在调用函数之前声明一个函数原型。 -
@Raildex:这是不正确的。这部分代码没有任何问题,只要在使用前声明函数,它就可以正常工作。
-
谢谢大家,它现在没有任何修改就可以工作了,我把函数定义移到了main()上面。