69,369
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
struct test
{
int key;
char value;
};
//根据索引号从test结构体数组中查询value的值
char Map(test str[], int index)
{
test *pointer = str;
while(pointer->value != '\0')//判断结构体数组有没有结束
{
if(index == pointer->key)
{
return pointer->value;
}
pointer++;
}
return NULL;//#define NULL 0 0是整型而返回类型是char类型,为什么不报错(环境是vs2010)
}
int main()
{
test a[3] = {{0,'a'},{1,'b'},{111,'\0'}};
int index;
scanf_s("%d",&index);
char result = Map(a,index);
if(0 != result)
{
printf("%c",result);
}
else
{
printf("This Number %d is not Esits",index);
}
printf("\nEND");
}