从 C 中的指针和指向指针的指针中删除元素

Remove element from a pointer and pointer to pointer in C

仍在学习,这是我正在处理的一段代码,我正在尝试从 pointer/pointer-pointer 中删除一个元素。问题接近代码末尾。

int total, tempX = 0;

printf("Input total people:\n");fflush(stdout);
scanf("%d",&total);
printf("You entered:  %i\n", total);

char **nAmer = (char**) malloc(total * sizeof(char*)); //pointer pointer for username
for (tempX=0; tempX<total; tempX++){
   nAmer[tempX] = malloc(21);
}

double *nUmer = (double*) malloc(total* sizeof(double)); //pointer for usernumber

printf("input their name and number:\n");fflush(stdout);

for (tempX = 0; tempX<total; tempX++){
    scanf("%20s %lf", nAmer[tempX], &nUmer[tempX]);
}

printf("Let me read that back:\n");
for (tempX = 0; tempX<total; tempX++){
   printf("Name: %s Number: %lf\n", nAmer[tempX], nUmer[tempX]);
}

char *searcher = (char*) malloc(21 * sizeof(char*)); //temporary string made by the user to compare names
printf("Enter name to remove user(s):\n");fflush(stdout);
scanf("%20s",searcher);
for (tempX = 0; tempX < total; tempX++){
    if (strcmp(searcher,nAmer[tempX])==0){ //what is better to replace this section?
       free(nAmer[tempX]); //I can assume this wont work well
       free(nUmer[tempX]); //I know this is a problem
   }
}
printf("Let me read that back with removed user(s):\n");fflush(stdout);
for (tempX = 0; tempX<total; tempX++){
    printf("Name: %s Number: %lf\n", nAmer[tempX], nUmer[tempX]);
}

我知道 free (nAmer[tempX]); 有效,但不允许在删除后回读。什么可以解决这个问题?

你不应该 free(nUmer[tempX]); 因为这不是指针。

当您释放其中一个名称指针时,您可以将其设置为NULL。然后打印可以跳过它的数组的循环。

for (tempX = 0; tempX < total; tempX++){
    if (strcmp(searcher,nAmer[tempX])==0){ //what is better to replace this section?
       free(nAmer[tempX]);
       nAmer[tempX] = NULL;
   }
}
printf("Let me read that back with removed user(s):\n");fflush(stdout);
for (tempX = 0; tempX<total; tempX++){
    if (nAmer[tempX]) {
        printf("Name: %s Number: %lf\n", nAmer[tempX], nUmer[tempX]);
    }
}

你还有一个错误:

char *searcher = (char*) malloc(21 * sizeof(char*)); //temporary string made by the user to compare names

这应该只是 * sizeof(char)(或者你可以忽略它,因为 sizeof(char) 被定义为 1)。

幸运的是,这分配了比需要更多的内存,而不是更少。

你有两个选择。

  1. 释放后nAmer中的指针改为NULL,不要改变nUmer。这样,在处理 nAmer[i]nUmer[i] 中的条目时,您可以检查 nAmer[i] 是否有效 (!=NULL) 或无效 (==NULL) 并忽略条目,如果无效。
  2. 您可以将数组中已删除条目之后的所有条目向上移动,请记住,现在没有 total 个条目,只有 total-1 个条目。

请不要free(nUmer[i])nUmer 中的条目是双精度而不是指针。你不能 free 他们。