为什么编译器一直要求我在不同类型中重新定义指针p、q、r?

huangapple go评论69阅读模式
英文:

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 &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

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-&gt;data = 23;
p-&gt;link = q;
p-&gt;link-&gt;data = 45;
q-&gt;link = NULL;
r = (NodePtr)malloc(sizeof(struct ListNode));
r-&gt;data = 34;
r-&gt;link = p-&gt;link;
p-&gt;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() 函数中调用它。
    • there is no link member in the ListNode structure. You probably mean next instead.
    • ListNode 结构中没有 link 成员。你可能指的是 next
  1. 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.

    • 还请注意,将指针隐藏在 typedef 后面会令人困惑且容易出错。指针是C内存模型的重要组成部分,将它们明确表示为 struct ListNode *pListNode *p 对大多数程序员来说提高了可读性。
  2. 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 the ListNode structure. You probably mean next 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 &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

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, &quot;cannot allocate list node\n&quot;);
        exit(1);
    }
    np-&gt;data = data;
    np-&gt;next = NULL;
    return np;
}

int main(void) {
    ListNode *p = new_node(23);
    ListNode *q = new_node(45);
    ListNode *r = new_node(34);

    r-&gt;next = q;
    p-&gt;next = r;

    free(p);
    free(q);
    free(r);
    return 0;
}

答案2

得分: 1

link 是从哪里来的?应该是 next。正如其他人所说,将其写入一个函数/主函数中。

英文:
p-&gt;link=q;

from where comes link? Should be next. as they others said write it into a function/main.

huangapple
  • 本文由 发表于 2023年2月24日 16:08:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75554012.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定