我有一些关于反转空终止的C字符串的概念性问题,并澄清了有关指针性质的问题。反转' 0'结尾的C字符串到位?
输入可能是
char arr[] = "opal";
和代码:
void reverse(char *str) { /* does *str = opal or does *str = o since the pointer str is type char? */
char* end = str; /* what is the difference between char* end and char *end? and is *end pointing to opal now? */
char tmp;
if (str) { /* if str isn't null? */
while (*end)
++end;
}
--end; /* end pointer points to l now */
while (str < end) { /* why not *str < *end? is this asking while o < l? */
tmp = *str; /* tmp = o */
*str++ = *end; /* what is the difference between *str++ and ++str? does *str++ = l? */
*end-- = tmp; /* *end points to o */
}
}
}
如果你真的在传递''opal'''这将是未定义的行为,因为你不允许修改字符串。 –
@ShafikYaghmour你会通过什么? – Opal
char数组,char arr [] =“opal”;'。 –