/* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */
#include
struct btbucode//定义结构体btbucode
{
int n;
char pn[50];
};
struct btbucode code[]={{11, "cailiao"},{12, "caiji"},{13, "shang"},{14, "jingji"},{15, "jixin"},
{16, "shipin"},{17, "lixueyuan"},{18, "fama"},{19, "waiguoyu"},{20, "yishuchuanmei"},
{95, "gonghui"},{96,"jiaowuchu"},{97,"renshichu"},{98,"kejichu"},{99,"xiaoban"}};//定义结构体数组code并初始化
/* PRESET CODE END - NEVER TOUCH CODE ABOVE */
#include
int main()
{int m,flag=0,i;
scanf("%d",&m);
for(i=0;i<strlen(code);i++)
if(m==code[i].n) {flag=1;printf("%s\n",code[i].pn);break;}
if(flag==0) printf("未找到\n");
return 0;}
我用了一个strlen函数来算结构体数组长度 但是出现错误提示
error C2664: 'strlen' : cannot convert parameter 1 from 'struct btbucode [15]' to 'const char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
code是结构体,不是字符串。
strlen的原理是从字符串开始位置往后寻找\0,作为字符串结束的标志得到字符串的长度。
因此对于一般数组,没有办法得到它的长度。
strlen是从字符串开始位置(你提供的指针)往后寻找\0,返回这个长度,用来计算你这个数组的长度肯定不行啊
#include
size_t strlen(const char *s);
The strlen() function calculates the length of the string s, excluding
the terminating null byte ('\0').