英文:
Why does compiler keep asking me to redefine pointer p, q, r in different type?
问题
#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int data;
struct ListNode *next;
};
typedef struct ListNode *NodePtr;
int main() {
NodePtr p, q, r;
p = (NodePtr)malloc(sizeof(struct ListNode));
q = (NodePtr)malloc(sizeof(struct ListNode));
p->data = 23;
p->next = q;
q->data = 45;
q->next = NULL;
r = (NodePtr)malloc(sizeof(struct ListNode));
r->data = 34;
r->next = p->next;
p->next = r;
free(p);
free(q);
free(r);
return 0;
}
上述代码创建了一个简单的链表结构,然后添加一个新节点并更改了相应的指针,最后释放了内存。
英文:
#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int data;
struct ListNode *next;
};
typedef struct ListNode *NodePtr;
NodePtr p, q, r;
p = (NodePtr)malloc(sizeof(struct ListNode));
q = (NodePtr)malloc(sizeof(struct ListNode));
p->data = 23;
p->link = q;
p->link->data = 45;
q->link = NULL;
r = (NodePtr)malloc(sizeof(struct ListNode));
r->data = 34;
r->link = p->link;
p->link = r;
free(p);
free(q);
free(r);
I just want to simply add a node and change the next pointer and free the pointer.
Please just give me a simple solution to fix.
答案1
得分: 2
以下是翻译好的部分:
-
- code cannot be executed at the global scope, you must enclose the code in a function definition and call it explicitly directly or indirectly from the
main()
function. - 代码不能在全局范围执行,必须将代码封装在一个函数定义中,并直接或间接地从
main()
函数中调用它。
- code cannot be executed at the global scope, you must enclose the code in a function definition and call it explicitly directly or indirectly from the
-
- there is no
link
member in theListNode
structure. You probably meannext
instead. ListNode
结构中没有link
成员。你可能指的是next
。
- there is no
-
Also note that it is confusing and error prone to hide pointers behind typedefs. Pointers are an important part of the C memory model, making them explicit as
struct ListNode *p
orListNode *p
improves readability for most programmers.- 还请注意,将指针隐藏在 typedef 后面会令人困惑且容易出错。指针是C内存模型的重要组成部分,将它们明确表示为
struct ListNode *p
或ListNode *p
对大多数程序员来说提高了可读性。
- 还请注意,将指针隐藏在 typedef 后面会令人困惑且容易出错。指针是C内存模型的重要组成部分,将它们明确表示为
-
Here is a modified version:
- 以下是修改后的版本:
#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int data;
struct ListNode *next;
};
typedef struct ListNode ListNode;
ListNode *new_node(int data) {
ListNode *np = malloc(sizeof(*np));
if (np == NULL) {
fprintf(stderr, "cannot allocate list node\n");
exit(1);
}
np->data = data;
np->next = NULL;
return np;
}
int main(void) {
ListNode *p = new_node(23);
ListNode *q = new_node(45);
ListNode *r = new_node(34);
r->next = q;
p->next = r;
free(p);
free(q);
free(r);
return 0;
}
希望这能帮助你明白原始代码中提到的问题和修改。
英文:
There are multiple issues in the posted code:
-
code cannot be executed at the global scope, you must enclose the code in a function definition and call it explicitly directly or indirectly from the
main()
function. -
there is no
link
member in theListNode
structure. You probably meannext
instead.
Also note that it is confusing and error prone to hide pointers behind typedefs. Pointers are an important part of the C memory model, making them explicit as struct ListNode *p
or ListNode *p
improves readability for most programmers.
Here is a modified version:
#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int data;
struct ListNode *next;
};
typedef struct ListNode ListNode;
ListNode *new_node(int data) {
ListNode *np = malloc(sizeof(*np));
if (np == NULL) {
fprintf(stderr, "cannot allocate list node\n");
exit(1);
}
np->data = data;
np->next = NULL;
return np;
}
int main(void) {
ListNode *p = new_node(23);
ListNode *q = new_node(45);
ListNode *r = new_node(34);
r->next = q;
p->next = r;
free(p);
free(q);
free(r);
return 0;
}
答案2
得分: 1
link 是从哪里来的?应该是 next。正如其他人所说,将其写入一个函数/主函数中。
英文:
p->link=q;
from where comes link? Should be next. as they others said write it into a function/main.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论