So for example I have a struct array with a lot of names in it and I have a pointer point to this struct. And I have a input to let user input the name to match the one in the struct what should I do without using the string and only use pointers?
所以例如我有一个结构数组,里面有很多名字,我有一个指向这个结构的指针。我有一个输入让用户输入名称以匹配结构中的名称我应该怎么做而不使用字符串只使用指针?
struct people{
char name[20];
}
people list[10];
people *ptr;
ptr = list;
char lists[20];
char *inpu;
inpu = lists;
cout << "Input name";
cin >> inpu;
I tried use this but its not working well.
我尝试过使用它,但效果不佳。
If ( inpu == (*ptr).name){
cout << "1";
}
else cout << "2";
1 个解决方案
#1
1
If you don't want to use strcmp, use totallynotstrcmp from here:
如果您不想使用strcmp,请从此处使用totallynotstrcmp:
int totallynotstrcmp(const char* s1, const char* s2)
{
while(*s1 && (*s1==*s2))
s1++,s2++;
return *(const unsigned char*)s1-*(const unsigned char*)s2;
}
#1
1
If you don't want to use strcmp, use totallynotstrcmp from here:
如果您不想使用strcmp,请从此处使用totallynotstrcmp:
int totallynotstrcmp(const char* s1, const char* s2)
{
while(*s1 && (*s1==*s2))
s1++,s2++;
return *(const unsigned char*)s1-*(const unsigned char*)s2;
}