Arrays 为什么可以';我是否将字符串按字符顺序存储到(char*)中?
因此,我尝试反转字符串并将其存储在Arrays 为什么可以';我是否将字符串按字符顺序存储到(char*)中?,arrays,c,string,algorithm,printf,Arrays,C,String,Algorithm,Printf,因此,我尝试反转字符串并将其存储在char*中。不知何故,我没有得到任何输出 为什么? #包括 int main(){ char arr[]=“XML”; int size=sizeof(arr)/sizeof(*arr);//size==4 字符buf[大小]; char*p=buf; 对于(int i=size;i>=0;i--){ snprintf(p,大小,“%c”,arr[i]); *p=*(p+1); } printf(“%s\n”,p); 返回0; } arr[i]在int i=
char*
中。不知何故,我没有得到任何输出
为什么?
#包括
int main(){
char arr[]=“XML”;
int size=sizeof(arr)/sizeof(*arr);//size==4
字符buf[大小];
char*p=buf;
对于(int i=size;i>=0;i--){
snprintf(p,大小,“%c”,arr[i]);
*p=*(p+1);
}
printf(“%s\n”,p);
返回0;
}
arr[i]
在int i=size之后代码>超出范围。初始化应为inti=size-2代码>(-1将指向终止的空字符,因此再减去一个)
*p=*(p+1)
正在将snprintf
放置的终止空字符分配给*p
。要移动指针,它应该是p=p+1代码>(或p++;
)
printf(“%s\n”,p)代码>将只看到终止的空字符。您应该改用buf
#include <stdio.h>
int main() {
char arr[] = "XML";
int size = sizeof(arr)/ sizeof(*arr); // size == 4
char buf[size];
char *p = buf;
for (int i=size-2; i>=0; i--){
snprintf(p, size-(p-buf),"%c", arr[i]);
p++;
}
printf("%s\n", buf);
return 0;
}
#包括
int main(){
char arr[]=“XML”;
int size=sizeof(arr)/sizeof(*arr);//size==4
字符buf[大小];
char*p=buf;
对于(int i=size-2;i>=0;i--){
snprintf(p,大小-(p-buf),%c',arr[i]);
p++;
}
printf(“%s\n”,buf);
返回0;
}
snprint
是胡说八道#include <stdio.h>
/* It reverses the string `str` */
/* If `newstr` is not NULL the result will be placed there and `str` will not be changed */
/* Otherwise it will ammend `str. In this case `str` has to be modifiable */
char *reverse(char *str, char *newstr)
{
size_t len = 0;
char *wrk = str;
if(str)
{
while(str[len]) len++;
if(newstr)
{
wrk = newstr;
for(size_t index = len; index; index--)
{
*newstr++ = str[index-1];
}
*newstr = 0;
}
else
{
if(*str)
{
char *end = str + len - 1;
while(end > str)
{
char tmp = *str;
*str++ = *end;
*end-- = tmp;
}
}
}
}
return wrk;
}
int main() {
char arr[] = "LMX";
int size = sizeof(arr)/ sizeof(*arr); // size == 4
char buf[size];
char *p = reverse(arr,buf);
printf("%s\n", p);
return 0;
}
#包括
/*它反转字符串“str”*/
/*如果'newstr'不为空,则结果将放在那里,并且'str'不会更改*/
/*否则它将变为'str'。在这种情况下,'str'必须是可修改的*/
char*反向(char*str,char*newstr)
{
尺寸长度=0;
char*wrk=str;
如果(str)
{
while(str[len])len++;
if(newstr)
{
wrk=newstr;
对于(大小索引=len;索引;索引--)
{
*newstr++=str[index-1];
}
*newstr=0;
}
其他的
{
如果(*str)
{
char*end=str+len-1;
while(end>str)
{
char tmp=*str;
*str++=*结束;
*结束--=tmp;
}
}
}
}
返回wrk;
}
int main(){
字符arr[]=“LMX”;
int size=sizeof(arr)/sizeof(*arr);//size==4
字符buf[大小];
char*p=反向(arr,buf);
printf(“%s\n”,p);
返回0;
}
1<代码>arr[i]在int i=size之后代码>超出范围。2. <代码>*p=*(p+1)代码>正在将\0
赋值给p[0]
,而不更新指针。size
包括终止null。所以你先打印那个字符,然后printf
看到一个长度为零的字符串……嘿,你能解释一下size-(p-buf)背后的逻辑吗?p-buf
是在计算p
前面有多少个元素。从size
中减去该值可防止写入超出范围。它不适用于空siring:。它还使用了snprintf,这是一种无意义的东西,它主要做所有的事情。@PacGrammer没有什么可分析的。这个程序非常糟糕,应该作为一个例子“如何在C语言中不反转字符串”。它也不处理角情况。@MikeCAT从大小中减去它可以防止写入超出范围。
引入另一个UB。同样糟糕
#include <stdio.h>
/* It reverses the string `str` */
/* If `newstr` is not NULL the result will be placed there and `str` will not be changed */
/* Otherwise it will ammend `str. In this case `str` has to be modifiable */
char *reverse(char *str, char *newstr)
{
size_t len = 0;
char *wrk = str;
if(str)
{
while(str[len]) len++;
if(newstr)
{
wrk = newstr;
for(size_t index = len; index; index--)
{
*newstr++ = str[index-1];
}
*newstr = 0;
}
else
{
if(*str)
{
char *end = str + len - 1;
while(end > str)
{
char tmp = *str;
*str++ = *end;
*end-- = tmp;
}
}
}
}
return wrk;
}
int main() {
char arr[] = "LMX";
int size = sizeof(arr)/ sizeof(*arr); // size == 4
char buf[size];
char *p = reverse(arr,buf);
printf("%s\n", p);
return 0;
}