Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 使用结构和数组_C_Arrays_Struct - Fatal编程技术网

C 使用结构和数组

C 使用结构和数组,c,arrays,struct,C,Arrays,Struct,我正试图学习如何表示一个图,遇到了这段代码,并试图理解它,所以我可以实现堆和dijkstras算法 我不熟悉此结构引用: graph->array[i].head = NULL; 这对结构合法吗?我在我的C语言书中没有看到这种东西,很有趣。这是因为代码为上行中的数组分配了足够的内存吗 graph->array = (struct AdjList*) malloc(V * sizeof(struct AdjList)); 代码的一部分,用于将内容放到上下文中。请帮忙,如果我真的试图

我正试图学习如何表示一个图,遇到了这段代码,并试图理解它,所以我可以实现堆和dijkstras算法

我不熟悉此结构引用:

graph->array[i].head = NULL;
这对结构合法吗?我在我的C语言书中没有看到这种东西,很有趣。这是因为代码为上行中的数组分配了足够的内存吗

graph->array = (struct AdjList*) malloc(V * sizeof(struct AdjList));

代码的一部分,用于将内容放到上下文中。请帮忙,如果我真的试图理解这种结构语法,我就无法理解为什么这种引用是可能的

  // A C Program to demonstrate adjacency list representation of graphs

#include <stdio.h>
#include <stdlib.h>

// A structure to represent an adjacency list node
struct AdjListNode
{
    int dest;
    struct AdjListNode* next;
};

// A structure to represent an adjacency list
struct AdjList
{
    struct AdjListNode *head;  // pointer to head node of list
};

// A structure to represent a graph. A graph is an array of adjacency lists.
// Size of array will be V (number of vertices in graph)
struct Graph
{
    int V;
    struct AdjList* array;
};

// A utility function to create a new adjacency list node
struct AdjListNode* newAdjListNode(int dest)
{
    struct AdjListNode* newNode =
            (struct AdjListNode*) malloc(sizeof(struct AdjListNode));
    newNode->dest = dest;
    newNode->next = NULL;
    return newNode;
}

// A utility function that creates a graph of V vertices
struct Graph* createGraph(int V)
{
    struct Graph* graph = (struct Graph*) malloc(sizeof(struct Graph));
    graph->V = V;

    // Create an array of adjacency lists.  Size of array will be V
    graph->array = (struct AdjList*) malloc(V * sizeof(struct AdjList));

     // Initialize each adjacency list as empty by making head as NULL
    int i;
    for (i = 0; i < V; ++i)
        graph->array[i].head = NULL;

    return graph;
}
//演示图形邻接列表表示的C程序
#包括
#包括
//表示邻接列表节点的结构
结构AdjListNode
{
int dest;
结构AdjListNode*下一步;
};
//表示邻接列表的结构
结构调整列表
{
struct AdjListNode*head;//指向列表头节点的指针
};
//表示图形的结构。图是邻接列表的数组。
//数组大小为V(图中的顶点数)
结构图
{
INTV;
结构调整列表*数组;
};
//用于创建新邻接列表节点的实用程序函数
结构AdjListNode*newAdjListNode(int dest)
{
结构AdjListNode*newNode=
(struct AdjListNode*)malloc(sizeof(struct AdjListNode));
newNode->dest=dest;
newNode->next=NULL;
返回newNode;
}
//创建V顶点图的实用函数
结构图*createGraph(int V)
{
结构图*图=(结构图*)malloc(sizeof(结构图));
图形->V=V;
//创建邻接列表数组。数组大小为V
图形->数组=(结构调整列表*)malloc(V*sizeof(结构调整列表));
//通过将head设置为NULL,将每个邻接列表初始化为空
int i;
对于(i=0;i数组[i]。head=NULL;
返回图;
}

此示例应能帮助您轻松理解

typedef struct ITEM
{
    int num;
} ITEM;

ITEM i;
i.num = 123; // this is what you already know

ITEM *p = &i; // p is pointer to i
p->num = 456; // to change value of struct pointer member, use '->'
// i.num is 456 now

(*p).num = 789; // alternative way
// i.num is 789 now

“我无法理解为什么这种引用是可能的”-为什么不可能呢?因为我不熟悉它:X,并且在网上找不到任何关于它的注释,你能告诉我吗?
[]
是数组索引,
是结构成员访问,
->
是取消引用加结构成员访问(
a->b
将与
(*a).b
)相同…并且它们只是链接在一起。因为,该语句将与
(((*graph.array)[i])同义。head=NULL;
,如果有帮助的话。这不是教程网站。如果您在解码标准方面有问题(并且不太复杂)标准C结构。你应该参考另一本书(教学法可能不适合你),或者你只是错过了关于
struct
s和指针的概念(显示的代码中没有数组)。@Siguza:
[]
是索引运算符。它应用于指针,而不是数组!对不起,我理解structs,它只是使用[]的部分,显然它是指针的索引运算符,从来没有遇到过这个问题before@Hyune如果您有
malloc
int*p
值为10 int,则执行
p[9]是完全有效的=123;
,因为它与
*(p+9)=123;
相同。