Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 为什么可以';我是否将字符串按字符顺序存储到(char*)中?_Arrays_C_String_Algorithm_Printf - Fatal编程技术网

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;
    }