数组不存储结构的地址
array not storing the address of structure
我正在尝试使用邻接表实现图形,据我所知,如果我创建变量 array
指向 struct adjlistnode
大小 v*sizeof(struct adjlistnode)
的指针,我会很薄可以在数组
的每个索引中存储vstruct adjlistnode
类型节点的地址
意味着数组的每个索引都将指向类型 struct adjlistnode
的节点但是当我分配 G->array[i]=NULL
时它给我错误
||=== Build: Debug in teeest (compiler: GNU GCC Compiler) ===|
C:\Users\Mahi\Desktop\DATA STR\teeest\main.c||In function 'creategraph':|
C:\Users\Mahi\Desktop\DATA STR\teeest\main.c|59|error: incompatible types when assigning to type 'struct adjlistnode' from type 'void *'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
why i am not able to assign NULL to index of array
如果我想访问邻接表,比如使用 G->array[i]
=第一个节点地址与图的第 i
个顶点相邻,我应该怎么做,稍后如果需要我将添加另一个节点
struct adjlistnode{
int dest;
struct adjlistnode* next;
};
struct graph{
int V;
struct adjlistnode* array;
};
struct adjlistnode* getnewnode(int dest){
struct adjlistnode* newnode =(struct adjlistnode*)malloc(sizeof(struct adjlistnode));
newnode->dest=dest;
newnode->next=NULL;
return newnode;
}
struct graph* creategraph(int v){
struct graph* G=(struct graph*)malloc(sizeof(struct graph));
G->V=v;
G->array=(struct adjlistnode*)malloc(v*sizeof(struct adjlistnode));
for(int i=0;i<v;i++){
G->array[i] =NULL;
}
return G;
}
array
是指向 struct adjlistnode
的(单个)指针。所以可以设置为NULL。
G->array = NULL; //is okay
但它不是指针数组,所以你不能访问数组的元素,也不能将它们设置为 NULL。
对于动态分配,你应该这样做:
struct graph{
int V;
struct adjlistnode** array;
};
struct graph* creategraph(int v){
struct graph* G = malloc(sizeof(struct graph));
G->V = v;
G->array = malloc(v * sizeof(struct adjlistnode*)); //allocation for an array of v pointers
for(int i = 0; i < v; i++){
G->array[i] = NULL;
}
return G;
}
正如@alk 所建议的,如果将 v
作为 size_t
而不是 int
传递会更好,因为 malloc
需要 size_t
。
G->array
的类型是 struct adjlistnode *
但是
G->array[i]
属于 struct adjlistnode
.
类型
因此您不能将 NULL
(void *
类型)分配给 struct adjlistnode
类型的 G->array[i]
您可能必须将 struct graph
中的 array
定义为 指向指针
的指针
struct graph{
int V;
struct adjlistnode** array;
};
然后以下内容应该适合您
struct graph* creategraph(int v){
struct graph* G=malloc(sizeof(struct graph));
G->V=v;
G->array=malloc(v*sizeof(struct adjlistnode*));
for(int i=0;i<v;i++){
G->array[i] =NULL;
}
return G;
}
** Note1(@alk 在评论中也提到)在 C 中,至少从 C89 标准开始,malloc
returns void *
。 void *
可以分配给任何其他指针类型(反之亦然),因此不需要强制转换 malloc
的 return 值。
** Note2(@alk 也指出)malloc
签名是用 size_t
类型的参数定义的,而不是 int
所以最好稍微修改一下代码并使用正确的类型(阅读 comparing int with size_t and size_t vs int in C++ and/or C 了解更多信息)
G->array[i]
returns *(array + i * sizeof(struct adjlistnode))
就好像 array
是 struct adjlistnode array[]
.
你所做的是存储 struct
的 v 个对象,但你尝试用 NULL 初始化它们,就像你初始化指针一样。
您可能想要的是
struct graph{
int V;
struct adjlistnode** array;
};
[...]
G->array=(struct adjlistnode**)malloc(v*sizeof(struct adjlistnode*));
这将使 array
成为指向指针数组的指针。
然后 G->array[i]
会 return 一个指向结构对象的 struct adjlistnode*
指针,然后你可以用你的 getnewnode()
.
初始化它
我正在尝试使用邻接表实现图形,据我所知,如果我创建变量 array
指向 struct adjlistnode
大小 v*sizeof(struct adjlistnode)
的指针,我会很薄可以在数组
struct adjlistnode
类型节点的地址
意味着数组的每个索引都将指向类型 struct adjlistnode
的节点但是当我分配 G->array[i]=NULL
时它给我错误
||=== Build: Debug in teeest (compiler: GNU GCC Compiler) ===|
C:\Users\Mahi\Desktop\DATA STR\teeest\main.c||In function 'creategraph':|
C:\Users\Mahi\Desktop\DATA STR\teeest\main.c|59|error: incompatible types when assigning to type 'struct adjlistnode' from type 'void *'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
why i am not able to assign NULL to index of array
如果我想访问邻接表,比如使用 G->array[i]
=第一个节点地址与图的第 i
个顶点相邻,我应该怎么做,稍后如果需要我将添加另一个节点
struct adjlistnode{
int dest;
struct adjlistnode* next;
};
struct graph{
int V;
struct adjlistnode* array;
};
struct adjlistnode* getnewnode(int dest){
struct adjlistnode* newnode =(struct adjlistnode*)malloc(sizeof(struct adjlistnode));
newnode->dest=dest;
newnode->next=NULL;
return newnode;
}
struct graph* creategraph(int v){
struct graph* G=(struct graph*)malloc(sizeof(struct graph));
G->V=v;
G->array=(struct adjlistnode*)malloc(v*sizeof(struct adjlistnode));
for(int i=0;i<v;i++){
G->array[i] =NULL;
}
return G;
}
array
是指向 struct adjlistnode
的(单个)指针。所以可以设置为NULL。
G->array = NULL; //is okay
但它不是指针数组,所以你不能访问数组的元素,也不能将它们设置为 NULL。
对于动态分配,你应该这样做:
struct graph{
int V;
struct adjlistnode** array;
};
struct graph* creategraph(int v){
struct graph* G = malloc(sizeof(struct graph));
G->V = v;
G->array = malloc(v * sizeof(struct adjlistnode*)); //allocation for an array of v pointers
for(int i = 0; i < v; i++){
G->array[i] = NULL;
}
return G;
}
正如@alk 所建议的,如果将 v
作为 size_t
而不是 int
传递会更好,因为 malloc
需要 size_t
。
G->array
的类型是 struct adjlistnode *
但是
G->array[i]
属于 struct adjlistnode
.
因此您不能将 NULL
(void *
类型)分配给 struct adjlistnode
G->array[i]
您可能必须将 struct graph
中的 array
定义为 指向指针
struct graph{
int V;
struct adjlistnode** array;
};
然后以下内容应该适合您
struct graph* creategraph(int v){
struct graph* G=malloc(sizeof(struct graph));
G->V=v;
G->array=malloc(v*sizeof(struct adjlistnode*));
for(int i=0;i<v;i++){
G->array[i] =NULL;
}
return G;
}
** Note1(@alk 在评论中也提到)在 C 中,至少从 C89 标准开始,malloc
returns void *
。 void *
可以分配给任何其他指针类型(反之亦然),因此不需要强制转换 malloc
的 return 值。
** Note2(@alk 也指出)malloc
签名是用 size_t
类型的参数定义的,而不是 int
所以最好稍微修改一下代码并使用正确的类型(阅读 comparing int with size_t and size_t vs int in C++ and/or C 了解更多信息)
G->array[i]
returns *(array + i * sizeof(struct adjlistnode))
就好像 array
是 struct adjlistnode array[]
.
你所做的是存储 struct
的 v 个对象,但你尝试用 NULL 初始化它们,就像你初始化指针一样。
您可能想要的是
struct graph{
int V;
struct adjlistnode** array;
};
[...]
G->array=(struct adjlistnode**)malloc(v*sizeof(struct adjlistnode*));
这将使 array
成为指向指针数组的指针。
然后 G->array[i]
会 return 一个指向结构对象的 struct adjlistnode*
指针,然后你可以用你的 getnewnode()
.