-1

グラフを作成しようとしていますが、このプログラムは、頂点のアドレスを保持する構造ポインタの配列を宣言した初期段階にあります *vertices[20] は、要素が 構造体ノード *を含む型のすべてのアドレスである配列ですグラフのノードのアドレス。

#include<stdio.h>
#include<stdlib.h>
struct node {
    int data;
    struct node *links[10];
};
struct node *create_node(int);
void create_vertices(struct node **);
int main()
{
    struct node *vertices[20];
    int d, i, choice;
        *vertices[0]=(struct node *)malloc(sizeof(struct node)*20);
        create_vertices (&vertices);
}

struct node *create_node(int data)
{
    int i;
    struct node *temp = (struct node *)malloc(sizeof(struct node));
    temp->data = data;
    for (i = 0; i < 10; i++)
        temp->links[i] = NULL;
    return temp;
}

void create_vertices (struct node **v)
{
 int i,choice;
 i=0;
    printf("enter choice\n");
    scanf("%d", &choice);
    while (choice == 1) {
        printf("enter data\n");
        scanf("%d", &d);
        vertices[i] = create_node(d);
        i++;
        printf("enter choice\n");
        scanf("%d", &choice);
    }
}

上記のコードをコンパイルすると、次のエラーが表示されます

bfs.c: In function ‘main’:
bfs.c:13:21: error: incompatible types when assigning to type ‘struct node’ from type ‘struct node *’
bfs.c:14:9: warning: passing argument 1 of ‘create_vertices’ from incompatible pointer type [enabled by default]
bfs.c:8:6: note: expected ‘struct node **’ but argument is of type ‘struct node * (*)[20]’
bfs.c: In function ‘create_vertices’:
bfs.c:35:16: error: ‘d’ undeclared (first use in this function)
bfs.c:35:16: note: each undeclared identifier is reported only once for each function it appears in
bfs.c:36:3: error: ‘vertices’ undeclared (first use in this function)

プログラムは次の行でエラーを言います

    struct node *vertices[20];
*vertices[0]=(struct node *)malloc(sizeof(struct node)*20);

この種の宣言の害は何ですか。タイプ struct node * のポインターの配列を宣言し、それらにもメモリーを与える必要があります。

4

1 に答える 1

0

主に:

create_vertices (&vertices);

次のようにする必要があります。

create_vertices (vertices);

また、 create_vertices() 関数に配列のサイズを知らせる方が安全です。i インデックスは、このサイズを超えることはできません。

更新:メインで、次の行を削除します:

*vertices[0]=(struct node *)malloc(sizeof(struct node)*20);

Vertices[] は、自動ストレージ (「スタック上」) 内の 20 個の (初期化されていない) ポインターの配列です。

create_vertices() 関数はポインタに値を与えます。(malloc() を呼び出し、その結果を vertices[i] に代入することにより)

于 2012-05-27T10:41:06.980 に答える