关于 Error: comparison '==' of pointer and int

本文通过两个示例函数对比了C语言中字符串与字符的比较方式,解释了为何使用.而非'.',并提供了正确的解决方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在程序时遇到了一个小问题,在这里记录一下吧,也为到这个问题的人节省点时间,哈哈哈哈哈。

出现问题的代码是这样的:

T_U8 Check_BookType(T_U16 *Str)
{
	T_U16 i;
	for(i=0;i<200;i++)
	{
		if((*Str) == ".")	//编译器提示问题出在这一句
		{
			//something...
			return 1;
		}
	}
	return 0;
}

巧的是,在这段代码前我也有类似的应用,如下:

T_U32 UnStr_To_Int(T_U16 *Str)
{
	T_U8 FoundNumFlag = 0;
	T_U32 Value = 0;
	while(*Str != '\0')
	{
		if(*Str >= '0' && *Str <= '9')
		{
			FoundNumFlag = 1;
			Value = Value * 10 + (*Str - '0');
		}
		else
		{
			if(FoundNumFlag == 1)
			{
				break;
			}
		}
		Str++;
	}
	if(FoundNumFlag == 1)
	{
		return Value;
	}
	else
	{
		return 0;
	}
}

但是这个函数完全没有报错,当时的我大概是比较瞎,愣是没有看出有什么问题!!!于是就百度了一下,但是并没有收获,很无奈。等下午的时候,就想着用必应搜看看,果然,必应第一个结果就是,网址如下:
https://stackoverflow.com/questions/32510218/warning-comparison-between-pointer-and-integer
有兴趣可以去看看。

总结

之所以会出现这个问题,是因为C语言里'"并不是一样的,'表示的是字符本身的值,"代表的是字符串,也就是" "其实是字符串的首地址,当我用"."的时候它并不是表示.的ASCII码值,而是"."这个字符串(就算只有一个字符也是字符串)的首地址。只有用'.'的时候它才是表示.的ASCII码值。

好像写的有点啰嗦了,不过希望能帮到遇到这个问题的人。

n file included from src/lab1/step1/step1.c:1:0: src/lab1/step1/../linklist.h: In function ‘addPoly’: src/lab1/step1/../linklist.h:62:7: error: expected identifier or ‘(’ before numeric constant PNODE 1=polyAddLeft->next; ^ src/lab1/step1/../linklist.h:65:8: warning: comparison between pointer and integer while(1!=NULL && r!=NULL) ^~ src/lab1/step1/../linklist.h:67:7: error: invalid type argument of ‘->’ (have ‘int) if(1->exp==r->exp) ^~ src/lab1/step1/../linklist.h:70:16: error: invalid type argument of ‘->’ (have ‘int) temp->exp=1->exp; ^~ src/lab1/step1/../linklist.h:71:17: error: invalid type argument of ‘->’ (have ‘int) temp->coef=1->coef+r->coef; ^~ src/lab1/step1/../linklist.h:75:8: error: invalid type argument of ‘->’ (have ‘int) 1=1->next; ^~ src/lab1/step1/../linklist.h:78:12: error: invalid type argument of ‘->’ (have ‘int) else if(1->exp < r->exp) ^~ src/lab1/step1/../linklist.h:81:16: error: invalid type argument of ‘->’ (have ‘int) temp->exp=1->exp; ^~ src/lab1/step1/../linklist.h:82:17: error: invalid type argument of ‘->’ (have ‘int) temp->coef=1->coef; ^~ src/lab1/step1/../linklist.h:86:8: error: invalid type argument of ‘->’ (have ‘int) 1=1->next; ^~ src/lab1/step1/../linklist.h:98:7: warning: comparison between pointer and integer if(1==NULL) sign=r; ^~ src/lab1/step1/../linklist.h:99:20: warning: assignment makes pointer from integer without a cast [-Wint-conversion] if(r==NULL) sign=1; ^ src/lab1/step1/../linklist.h: In function ‘printPoly’: src/lab1/step1/../linklist.h:132:7: error: expected ‘;’ before ‘printf’ printf("%dx^%d",p->coef,p->exp) ^~~~~~
最新发布
03-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值