有没有在C函数会返回一个char的索引字符数组? [英] Is there a function in c that will return the index of a char in a char array?
本文介绍了有没有在C函数会返回一个char的索引字符数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有在C函数会返回一个char的索引字符数组?
例如是这样的:
char值[] =0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ;
焦炭找到='E';INT指数= findInedexOf(价值观,找到);
解决方案
和strchr
返回指向第一次出现,所以要找到索引,只取随偏移距起始指针。例如:
char值[] =0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ;
焦炭找到='E';为const char * PTR =和strchr(价值观,找到);
如果(PTR){
INT指数= PTR - 值;
// 做一点事
}
Is there a function in c that will return the index of a char in a char array?
For example something like:
char values[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char find = 'E';
int index = findInedexOf( values, find );
解决方案
strchr
returns the pointer to the first occurrence, so to find the index, just take the offset with the starting pointer. For example:
char values[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char find = 'E';
const char *ptr = strchr(values, find);
if(ptr) {
int index = ptr - values;
// do something
}
这篇关于有没有在C函数会返回一个char的索引字符数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文