【发布时间】:2017-06-04 18:39:21
【问题描述】:
我是一个菜鸟学生,正在尝试编写一个使用二叉搜索树来组织公司员工的程序。我的老师告诉我,如果我希望能够创建 Worker 结构的新实例,我可以将 malloc 与该结构一起使用,每次使用时都会返回指向新结构的指针,然后我可以编辑该新结构的详细信息结构来自另一个函数。但是我该怎么做呢?无论我做什么,它都会变得如此复杂,我无法做到。这是我能够编写这部分代码的代码,只是为了测试我是否可以创建和编辑新结构。 我要问的主要问题是,如何编辑新创建的结构?
#include<stdlib.h>
#include<stdio.h>
struct btnode
{
int value = 5;
struct btnode *l;
struct btnode *r;
};
int test(int *p)
{
printf("%d", &p->value);
}
int main()
{
int *asdf = (int *)malloc(sizeof(struct btnode));
test(asdf);
}
【问题讨论】:
-
除此之外,
printf("%d", &p->value);--> 是时候重新阅读本章以获得指针了。 -
int *asdf = (int *)malloc(sizeof(struct btnode));==>struct btnode *asdf = malloc(sizeof *asdf); -
我只是在测试,只是在绝望中尝试不同的东西。哦,我承认,我很难理解指针。
-
您的老师对 C 和 C++ 之间的区别感到困惑。您不应该在 C++ 中使用
malloc。你应该使用new。
标签: c pointers struct structure