[英]Remove element from a pointer and pointer to pointer in C
Still learning, this is a segment of code I'm working on and I'm trying to remove an element(s) from a pointer/pointer-pointer. 仍在学习中,这是我正在处理的一段代码,我正在尝试从指针/指针-指针中删除一个或多个元素。 The problem is near the end of the code. 问题接近代码的结尾。
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]);
}
I know free (nAmer[tempX]);
我知道free (nAmer[tempX]);
works but doesn't allow for the read back after its removal. 可以,但是删除后不允许回读。 What would fix this? 什么会解决这个问题?
You shouldn't free(nUmer[tempX]);
你不应该free(nUmer[tempX]);
because this isn't a pointer. 因为这不是指针。
When you free one of the name pointers, you can set it to NULL
. 当释放名称指针之一时,可以将其设置为NULL
。 Then the loop that prints the array that can skip it. 然后循环打印可以跳过它的数组。
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]);
}
}
You have another mistake: 您还有另一个错误:
char *searcher = (char*) malloc(21 * sizeof(char*)); //temporary string made by the user to compare names
This should just be * sizeof(char)
(or you can just leave this out, since sizeof(char)
is defined to be 1
). 它应该只是* sizeof(char)
(或者您可以将其省略,因为sizeof(char)
被定义为1
)。
Luckily this allocates more memory than needed, not less. 幸运的是,这会分配比所需更多的内存,而不是更少。
You have two options. 您有两个选择。
nAmer
to NULL
after you freed it, don't change nUmer
. 释放它后,将nAmer
的指针更改为NULL
,不要更改nUmer
。 That way when processing an entry in nAmer[i]
or nUmer[i]
you can check if nAmer[i]
is valid ( !=NULL
) or not ( ==NULL
) and ignore the entry if it is invalid. 这样,当在nAmer[i]
或nUmer[i]
处理条目时,您可以检查nAmer[i]
是否有效( !=NULL
)或无效( ==NULL
)并忽略该条目(如果无效)。 total
number of entries but only total-1
number of entries. 您可以将删除的条目后的所有条目在数组中上移一个,并记住现在没有条目total
,只有条目total-1
。 Please do not free(nUmer[i])
. 请不要free(nUmer[i])
。 The entries in nUmer
are doubles and not pointers. nUmer
中的条目是双精度而不是指针。 You cannot free
them. 您无法free
它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.