如何从命令行 argv 动态分配多个字符串?
How do I dynamically allocate multiple strings from the command line argv?
对于这个作业,我应该从命令行参数中获取多个名称,这些名称以“+”(将名称插入我的链表)或“-”(从列表),所以如果我要输入“+bill +jim +ted -bill”,我会添加这三个不带“+”的名字并删除 bill。我尝试使用 malloc
动态分配一个字符串,这样我就可以修改 name
字符串,但是我的代码没有将字符串数据插入到我的链表中,我得到一个 free(): invalid pointer 错误我通过了一些测试字符串。如果我将 free(name)
语句移动到 if/else if 语句,我会遇到分段错误。我将如何从命令行正确地动态分配这些字符串以便对其进行修改?
int main(int argc, char *argv[]){
struct node* head;
for(int x = 0; x < argc; x++){
char * name = malloc((strlen(argv[x])+1));
name = argv[x];
if(name[0] == '+'){
printf("adding %s \n", name++);
insert(&head, name++);
printf("List: ");
printList(&head);
}
else if(name[0] == '-'){
printf("removing %s \n", name++);
removeNode(&head, name++);
}
free(name);
}
}
char * removeNode(struct node** head, char* name){
struct node *current = *head;
struct node *previous;
if(current == NULL){
return "error0";
}
if(current->data == name){
struct node * node2delete= *head;
*head = node2delete->next;
char * name2 = malloc(strlen(name)+1);
strcpy(name2, name);
free(node2delete);
printf("Removed %s \n", name);
return name2;
}
while(current != NULL){
previous = current;
current = current->next;
if(current->data == name){
previous->next = current->next;
char * name2 = malloc(strlen(name)+1);
strcpy(name2, name);
free(current);
printf("Removed %s \n", name);
return name2;
}
}
return "error0";
}
尝试使用 strcpy(name, argv[x]);
而不是 name = argv[x]
,因为当您将指针分配给指针时,它们将指向存储空终止字符数组的相同内存位置。这样您就可以在调用 free(name);
时为 argv[x] 完成内存释放
指针操作与赋值不同
另请参阅此线程:How do malloc() and free() work?
很多问题
字符串复制不正确
//char * name = malloc((strlen(argv[x])+1));
//name = argv[x];
char * name = malloc(strlen(argv[x])+1);
if (name == NULL) return EXIT_FAILURE; // Out of memory
strcpy(name, argv[x]);
不正确free()
代码需要释放从分配中接收到的相同值。 name++
搞砸了。
未初始化的磁头*
// struct node* head;
struct node* head == NULL;
更多
可能只需要在添加的时候分配,在+/-
之后分配。
int main(int argc, char *argv[]){
struct node* head = NULL;
// for(int x = 0; x < argc; x++){
for(int x = 1; x < argc; x++){ // Skip adding the program name, start at 1
if(argv[x][0] == '+'){
char * name = malloc((strlen(argv[x] + 1)+1));
if (name == NULL) return EXIT_FAILURE; // Out of memory
strcpy(name, argv[x]);
printf("adding %s \n", name); // No ++
insert(&head, name); // No ++
printf("List: ");
printList(&head);
}
else if(argv[x][0] == '-'){
printf("removing %s \n", argv[x] + 1);
// This is a problem, I'd expect removeNode() to return a pointer to the free'd name
char *name = removeNode(&head, argv[x] + 1);
free(name);
}
}
对于这个作业,我应该从命令行参数中获取多个名称,这些名称以“+”(将名称插入我的链表)或“-”(从列表),所以如果我要输入“+bill +jim +ted -bill”,我会添加这三个不带“+”的名字并删除 bill。我尝试使用 malloc
动态分配一个字符串,这样我就可以修改 name
字符串,但是我的代码没有将字符串数据插入到我的链表中,我得到一个 free(): invalid pointer 错误我通过了一些测试字符串。如果我将 free(name)
语句移动到 if/else if 语句,我会遇到分段错误。我将如何从命令行正确地动态分配这些字符串以便对其进行修改?
int main(int argc, char *argv[]){
struct node* head;
for(int x = 0; x < argc; x++){
char * name = malloc((strlen(argv[x])+1));
name = argv[x];
if(name[0] == '+'){
printf("adding %s \n", name++);
insert(&head, name++);
printf("List: ");
printList(&head);
}
else if(name[0] == '-'){
printf("removing %s \n", name++);
removeNode(&head, name++);
}
free(name);
}
}
char * removeNode(struct node** head, char* name){
struct node *current = *head;
struct node *previous;
if(current == NULL){
return "error0";
}
if(current->data == name){
struct node * node2delete= *head;
*head = node2delete->next;
char * name2 = malloc(strlen(name)+1);
strcpy(name2, name);
free(node2delete);
printf("Removed %s \n", name);
return name2;
}
while(current != NULL){
previous = current;
current = current->next;
if(current->data == name){
previous->next = current->next;
char * name2 = malloc(strlen(name)+1);
strcpy(name2, name);
free(current);
printf("Removed %s \n", name);
return name2;
}
}
return "error0";
}
尝试使用 strcpy(name, argv[x]);
而不是 name = argv[x]
,因为当您将指针分配给指针时,它们将指向存储空终止字符数组的相同内存位置。这样您就可以在调用 free(name);
指针操作与赋值不同
另请参阅此线程:How do malloc() and free() work?
很多问题
字符串复制不正确
//char * name = malloc((strlen(argv[x])+1));
//name = argv[x];
char * name = malloc(strlen(argv[x])+1);
if (name == NULL) return EXIT_FAILURE; // Out of memory
strcpy(name, argv[x]);
不正确free()
代码需要释放从分配中接收到的相同值。 name++
搞砸了。
未初始化的磁头*
// struct node* head;
struct node* head == NULL;
更多
可能只需要在添加的时候分配,在+/-
之后分配。
int main(int argc, char *argv[]){
struct node* head = NULL;
// for(int x = 0; x < argc; x++){
for(int x = 1; x < argc; x++){ // Skip adding the program name, start at 1
if(argv[x][0] == '+'){
char * name = malloc((strlen(argv[x] + 1)+1));
if (name == NULL) return EXIT_FAILURE; // Out of memory
strcpy(name, argv[x]);
printf("adding %s \n", name); // No ++
insert(&head, name); // No ++
printf("List: ");
printList(&head);
}
else if(argv[x][0] == '-'){
printf("removing %s \n", argv[x] + 1);
// This is a problem, I'd expect removeNode() to return a pointer to the free'd name
char *name = removeNode(&head, argv[x] + 1);
free(name);
}
}