Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.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
c语言中指针到指针的赋值_C_Pointers - Fatal编程技术网

c语言中指针到指针的赋值

c语言中指针到指针的赋值,c,pointers,C,Pointers,为什么这个代码不起作用 #include<stdio.h> #include<stdlib.h> int main(int argc,char **argv){ char * new; new = malloc(10); const char * s1 = "hello"; while(*s1){ *new++ = *s1++; } printf("%s",new); return 0; }

为什么这个代码不起作用

#include<stdio.h>
#include<stdlib.h>

int main(int argc,char **argv){

    char * new;
    new = malloc(10);
    const char * s1 = "hello";

    while(*s1){
        *new++ = *s1++;
    }
    printf("%s",new);

    return 0;
}
#包括
#包括
int main(int argc,字符**argv){
字符*新;
新=malloc(10);
const char*s1=“你好”;
while(*s1){
*新+++=*s1++;
}
printf(“%s”,新);
返回0;
}

我运行了gdb,发现*s1没有分配给*new。为什么?

new
指向从
s1
复制的任何内容的最后一个元素后的一个字节。您应该将指针传递到
new
的第一个元素,以获取整个文本

另一个问题是,您没有将
\0
s1
复制到
new
。在尝试打印之前,应在“新建”的末尾添加一个
\0
。否则,您将调用

您可以这样做并检查:

#include<stdio.h>
#include<stdlib.h>

int main(int argc,char **argv){

    char * new, *new2;
    new = malloc(10);
    new2 = new;
    const char * s1 = "hello";
    while(*s1){
        printf("%c\n", *s1); // the \0 is *not* appended
        *new++ = *s1++;
    }
    *new = '\0';
    printf("%s\n",new2);
    return 0;
}
#包括
#包括
int main(int argc,字符**argv){
字符*new,*new2;
新=malloc(10);
new2=新的;
const char*s1=“你好”;
while(*s1){
printf(“%c\n”,*s1);//未追加\0
*新+++=*s1++;
}
*新='\0';
printf(“%s\n”,new2);
返回0;
}

new
指向从
s1
复制的任何内容的最后一个元素后的一个字节。您应该将指针传递到
new
的第一个元素,以获取整个文本

另一个问题是,您没有将
\0
s1
复制到
new
。在尝试打印之前,应在“新建”的末尾添加一个
\0
。否则,您将调用

您可以这样做并检查:

#include<stdio.h>
#include<stdlib.h>

int main(int argc,char **argv){

    char * new, *new2;
    new = malloc(10);
    new2 = new;
    const char * s1 = "hello";
    while(*s1){
        printf("%c\n", *s1); // the \0 is *not* appended
        *new++ = *s1++;
    }
    *new = '\0';
    printf("%s\n",new2);
    return 0;
}
#包括
#包括
int main(int argc,字符**argv){
字符*new,*new2;
新=malloc(10);
new2=新的;
const char*s1=“你好”;
while(*s1){
printf(“%c\n”,*s1);//未追加\0
*新+++=*s1++;
}
*新='\0';
printf(“%s\n”,new2);
返回0;
}

因为
新的
指向从
s1
复制到
新的
的最后一个字符后面的位置

你可以做:

#include<stdio.h>
#include<stdlib.h>

int main(int argc,char **argv){

    char *new, *tmp;
    new = malloc(10);
    if (new == NULL){
        printf ("Failed to allocate memory");
        return -1;
    }

    const char * s1 = "hello";
    tmp = new;   //take a temporary pointer and point it to new
    while(*s1){
       *tmp++ = *s1++;    //use tmp to copy data
    }
    *tmp = '\0';
    printf("%s",new);
    return 0; 
}
#包括
#包括
int main(int argc,字符**argv){
字符*新,*tmp;
新=malloc(10);
if(new==NULL){
printf(“分配内存失败”);
返回-1;
}
const char*s1=“你好”;
tmp=new;//获取一个临时指针并将其指向new
while(*s1){
*tmp++=*s1++;//使用tmp复制数据
}
*tmp='\0';
printf(“%s”,新);
返回0;
}

因为
新的
指向从
s1
复制到
新的
的最后一个字符后面的位置

你可以做:

#include<stdio.h>
#include<stdlib.h>

int main(int argc,char **argv){

    char *new, *tmp;
    new = malloc(10);
    if (new == NULL){
        printf ("Failed to allocate memory");
        return -1;
    }

    const char * s1 = "hello";
    tmp = new;   //take a temporary pointer and point it to new
    while(*s1){
       *tmp++ = *s1++;    //use tmp to copy data
    }
    *tmp = '\0';
    printf("%s",new);
    return 0; 
}
#包括
#包括
int main(int argc,字符**argv){
字符*新,*tmp;
新=malloc(10);
if(new==NULL){
printf(“分配内存失败”);
返回-1;
}
const char*s1=“你好”;
tmp=new;//获取一个临时指针并将其指向new
while(*s1){
*tmp++=*s1++;//使用tmp复制数据
}
*tmp='\0';
printf(“%s”,新);
返回0;
}

您在循环中修改了
new
,因此它不再指向您分配的块的开头,而是指向您复制的最后一个块之后的字符。您还没有复制空终止符。您在循环中修改了
new
,因此它不再指向您分配的块的开头,而是指向您复制的最后一个块之后的字符。你还没有复制空终止符。啊,我想接受两个答案!谢谢你的时间,我想接受两个答案!感谢您的时间PPLAN还记得,<代码>新< /C>可能不是您的最佳选择名称(虽然合法),因为<代码>新< /Cord>是在C++中分配的。代码>字符*p=新和在
p
上操作同样有效。你说得对@DavidC.Rankin会改变的。。律师和程序员你也是一个灵感来源!谢谢你,只是双极性恋(左脑/右脑的事情
:)
@DavidC.Rankin双极性恋让你变得多才多艺什么??还记得<<代码>新< /C>可能不是你名字的最佳选择(虽然合法),因为<代码>新< /COD>是在C++中分配的。代码>字符*p=新和在
p
上操作同样有效。你说得对@DavidC.Rankin会改变的。。律师和程序员你也是一个灵感来源!谢谢你,只是双极性恋(左脑/右脑的事情
:)
@DavidC.Rankin双极性恋让你变得多才多艺什么??英雄联盟