C中用于获取字符串的指针算术 [英] pointer arithmetic in C for getting a string
本文介绍了C中用于获取字符串的指针算术的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想获取一个字符数组的元素,但根本没有成功,问题是我只获取了第一个和最后一个元素,仅此而已,我的代码是:
I want to get the elements of an array of characters, but no success at all, the problem is that I only get the first and last element and nothing more, my code is:
void getcharacters(char *cad)
{
int l;
int *i;
l=strlen(cad);
for (i=&cad[0];i<&cad[l];i++){
printf("%c\n",*cad);
}
}
有什么帮助吗? 谢谢
推荐答案
您为所有printf使用相同的字符(*cad
或cad[0]
).您需要使用索引来获取每次迭代中的下一个字符.我也需要成为char的指针:
You are using the same character (*cad
or cad[0]
) for all printf's. What you need is to use the index to get the next char in each iteration. Also i needs to be a pointer to char:
void getcharacters(char *cad)
{
int l;
char *i;
l=strlen(cad);
for (i=&cad[0];i<&cad[l];i++){
printf("%c\n", *i );
}
}
这篇关于C中用于获取字符串的指针算术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文