C语言出错,求救啊

来源:百度知道 编辑:UC知道 时间:2024/09/23 13:19:29
#include<stdio.h>
#include<string.h>
void main()
{
char *a="zj";
char *b;
printf("who am I");
gets(b);
if(a==b)
printf("that`s right\n");
else
printf("NO\n");
}
如果那个 char*a='zj' 我想多点字符呢,好像就不行了。

呵呵,你是没有弄明白指针的定义。指针里面存放的是地址,比如你上面的char *a="zj",指针a指向字符串zj,指针a里面放的是zj在内存里面的地址;而指针b指向的是你所输入的字符串,你输入字符后内存会给你输入的分配一个地址,所以无论你输入的是什么,地址永远不一样,所以a和b永远不相等。
如果想实现你所要达到的功能,应该这样改:
#include<stdio.h>
#include<string.h>
void main()
{
char *a="zj";
char *b;
printf("who am I");
gets(b);
if(*a==*b) \*注意这里*\
printf("that`s right\n");
else
printf("NO\n");
}

c中 判断字符串a和b是否相等不能用if(a==b)
应该用字符串比较函数 strcmp

if (strcmp(a, b) == 0)

1 单个字符变亮不能存多个符号
2 字符串不能直接比较
可改为
#include<stdio.h>
#include<string.h>
void main()
{
char a[10]="zj";
char b[10];
printf("who am I\n");
gets(b);
if(a[0]==b[0]&&a[1]==b[1])
printf("that`s right\n");
else
printf("NO\n");
}

#include<stdio.h>