Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.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_Segmentation Fault_Double Pointer - Fatal编程技术网

C 尝试使用双指针复制字符串时出现分段错误

C 尝试使用双指针复制字符串时出现分段错误,c,segmentation-fault,double-pointer,C,Segmentation Fault,Double Pointer,Jus开始学习指针,我被这个程序输出分段错误所困扰。 它应该将字符串的前10个字符复制到双指针指向的位置 使用gdb我发现**pt=*s;产生seg故障 #include <stdio.h> #include <stdlib.h> void str1(char *s, char **pt); void str1(char *s, char **pt){ for(int i=0;i<10;i++){ **pt=*s; pt++;

Jus开始学习指针,我被这个程序输出分段错误所困扰。 它应该将字符串的前10个字符复制到双指针指向的位置 使用gdb我发现**pt=*s;产生seg故障

#include <stdio.h>
#include <stdlib.h>
void str1(char *s, char **pt);
void str1(char *s, char **pt){
    for(int i=0;i<10;i++){
        **pt=*s;
        pt++;
        s++;

    }
}
int main (void) {
   char str[30] = "223This is test";
   char *ptr;
   str1(str, &ptr);
   printf("%s", ptr);
   return 0;
}

你可能想要这个:

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

void str1(char* s, char** pt) {
  char *p = malloc(100);           // allocate memory for destination
  *pt = p;                         // store it for the caller

  for (int i = 0; i < 10; i++) {
    *p = *s;
    p++;
    s++;
  }

  *p = 0;     // put string terminator, otherwise printf won't work correctly
}

int main(void) {
  char str[30] = "223This is test";
  char *ptr;                       // for now p points nowhere
  str1(str, &ptr);                 // now p points to the memory allocated in str1
  printf("%s", ptr);
  free(ptr);                       // free memory for completeness
  return 0;
}
首先,ptr没有初始化,只有为它保留空间或在其中存储有效的内存地址,即使它指向某个有效变量,才能真正使用它

char *ptr = malloc(11);
然后您需要在函数中适当地增加它:

(*pt)++;
复制完成后,您需要以null终止字符数组,以便将其视为字符串,也称为以null终止的字符数组

**pt = '\0';
现在,由于ptr是作为指向指针的指针传递的,因此调用方知道增量,在本例中为main,因此当您尝试打印它时,它不会打印任何内容,因为它指向char数组的末尾,我们需要将其返回到begging

*pt -= 10;
已更正的代码,注释以您的为基础:

#include <stdio.h>
#include <stdlib.h>
#define SIZE 10

void str1(char *s, char **pt) {
    for (int i = 0; i < SIZE; i++) {
        **pt = *s;
        (*pt)++; //properly increment pt
        s++;
    }
    **pt = '\0'; //null terminate copied string

    //since ptr was passed as **, the increment is known by the caller
    //now ptr will be pointing to the end of the string
    //we have to bring it back to the beginning
    *pt -= SIZE;
}

int main(void) {

    char str[] = "223This is test";  
    char *ptr = malloc(SIZE + 1); //space for 10 character + null-terminator

    //check for allocation errors
    if(ptr == NULL){
        perror("malloc");
        return EXIT_FAILURE;
    }

    str1(str, &ptr);
    printf("%s", ptr); 
    free(ptr);
    return EXIT_SUCCESS;
}