C字符串 [英] C Strings
问题描述
你能走过吗? C字符串或字符串指针(使用*(sz + 1)),例如
,你可以使用数组。如果没有,为什么不呢?如果是这样,怎么样?
cman
Can you "walk across" C strings or char pointers (using *(sz+1)) like
you can with arrays. If not, why not? If so, how?
cman
推荐答案
cman写道:
cman wrote:
你能走过吗? C字符串或字符串指针(使用*(sz + 1)),例如
,你可以使用数组。如果没有,为什么不呢?如果是这样,怎么样?
Can you "walk across" C strings or char pointers (using *(sz+1)) like
you can with arrays. If not, why not? If so, how?
查看常见问题解答,您最喜欢的好C书或Google。这是基本的C.
想想:
* sz ++
Check the FAQ, your favourite good C book or Google. This is basic C.
Think:
*sz++
cman写道:
cman wrote:
你能走过吗? C字符串或字符串指针(使用*(sz + 1)),例如
,你可以使用数组。
Can you "walk across" C strings or char pointers (using *(sz+1)) like
you can with arrays.
是的,只要你保持在阵列的范围内,你就会踩到
到。
Yes, provided you stay within the bounds of the array you''re stepping
through.
" cman" < ti **** @ gmail.com在消息中写了
"cman" <ti****@gmail.comwrote in message
你能走过吗? C字符串或字符串指针(使用*(sz + 1)),例如
,你可以使用数组。如果没有,为什么不呢?如果是这样,怎么样?
Can you "walk across" C strings or char pointers (using *(sz+1)) like
you can with arrays. If not, why not? If so, how?
基本上没有C字符串这样的东西。
字符串是一个字符数组,有一个NUL来表示作为哨兵或终结点
字符。
所以
char * astring =" Hello Fred" ;;
char ch = astring [1];
将ch设置为''e'';
类似地,您可以设置指向字符串中任何点的指针,并且
递增或递减它以移动字符串中的字符。例如
int countchar(char * str,char ch)
{
int answer = 0;
while(* str)
{
if(* str ++ == ch)
answer ++;
}
返回答案;
}
There is essentially no such thing as a C string.
Strings are arrays of chars with a NUL to act as a sentinel or termination
character.
so
char *astring = "Hello Fred";
char ch = astring[1];
sets ch to ''e'';
Similarly you can set a pointer to any point within the string, and
increment or decrement it to move through the characters of the string. Eg
int countchar(char *str, char ch)
{
int answer = 0;
while(*str)
{
if(*str++ == ch)
answer++;
}
return answer;
}
这篇关于C字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!