以星号结尾的 C 结构

标签 c pointers struct

我在看这个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/

相关文章:

c - 指针操作期间出现段错误

c# - 为什么 System.Windows.Point 和 System.Windows.Vector 是可变的?

C# 导入 C Dll。结构中的数组指针。如何?

c - 在C中寻找迷宫的最小路径

c - 将 kmalloc 内存映射到用户空间

c - Linux C : segfault error 4 in libmysqlclient. so.18.0.0

c - 指向数组的指针 - C 编程

c - 为什么我们不能在不使用大括号的情况下在 switch case 冒号之后声明变量?

c++ - c++ 类实例成员是如何在机器级别传递的?

c++ - 如何在调试期间取消引用指针