gpt4 book ai didi

以星号结尾的 C 结构

转载 作者:太空狗 更新时间:2023-10-29 15:17:23 24 4
gpt4 key购买 nike

我在看这个example我发现有声明

struct edge
{
int x;
int y;
int weight;
struct edge *link;
}*front = NULL;

这到底是什么意思?是否可以创建一个结构,该结构也是一个名为 front 且为 NULL... 的指针?

最佳答案

结构只是另一种 C 类型,因此,它用于定义的变量可以创建为普通实例或指针:

int a, *pA=NULL; //normal instance, pointer instance

struct edge
{
int x;
int y;
int weight;
struct edge *link;
}sEdge, *front = NULL; //normal instance, pointer instance

而且,与任何指针变量一样,在安全使用之前需要指向拥有的内存:(示例)

int main(void)
{

// both variable types are handled the same way...

pA = &a; //point pointer variable to normal instance of `int a`
front = &sEdge;//point pointer `front` to instance of 'struct edge'

//allocate memory, resulting in assigned address with associated memory.
pA = malloc(sizeof(*pA));
front = malloc(sizeof(*front));
...

编辑 回答评论中的问题:
这个小例子没有抛出任何错误或警告。 (在上面编辑您的问题,或者更好的是,发布另一个显示您所看到内容的详细信息的问题。)

struct edge
{
int x;
int y;
int weight;
struct edge *link;
}*front = '\0';

int main(void)
{
struct edge tree[10];

return 0;
}

关于以星号结尾的 C 结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50686247/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com