質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

Q&A

1回答

1304閲覧

起点ノードから他のノードへの距離を計算したい

rft3

総合スコア7

C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

0グッド

0クリップ

投稿2020/06/14 13:43

編集2020/06/14 14:16

キューを用いた幅優先探索、構造体のソートの勉強をしています。起点ノードから他のノードへの距離を計算するために、 calc_distance関数の引数やキューの要素をNode構造体へのポインタとして、Nodeのメンバ変数degree, adj_nodes, nameを用いたプログラムを作りたいのですが、エラーが出てしまいコンパイルが出来ません。どこが間違えているか教えていただきたいです。

C

1#include <stdio.h> 2#include <stdlib.h> 3#include "graph_struct.h" 4#include "queue.h" 5 6Node** nodes_ptr; 7 8int *disV; //距離ベクトル 9Queue queue; //幅優先探索用のキュー 10int adj_nodes; 11Node degree; 12Node name; 13 14int init_value(){ 15 init_queue(&queue, N); 16 disV = (int *) malloc(sizeof(int)*N); 17} 18void calc_distance(nodes_ptr origin){ 19 queue.first = queue.last = 0; 20 enq(&queue, origin); 21 22 for(int i = 0; i < N; i++) disV[i] = id-1; 23 disV[id-1] = 0; 24 25 int maxD = 0; //最大距離 26 for(int d = 0; ; d++){ 27 int h1 = queue.first, h2 = queue.last; 28 for(int h = h1; h < h2; h++){ 29 30 nodes_ptr node = deq(&queue); 31 for(int k = 0; k < degree[node]; k++){ 32 nodes_ptr adj_node = adj_nodes[node][k]; 33 if(degree[adj_node] == -1){ 34 disV[adj_node] = d+1; 35 enq(&queue, adj_node); 36 } 37 } 38 } 39 if(queue.first == N || queue.first == queue.last){ maxD = d; break; } 40 } 41 return maxD; 42} 43 44 45void print_distance(char *fn, int D){ 46 FILE *fp = fopen(fn, "w"); 47 int *hashV = (int *) calloc(D+1, sizeof(int)); 48 for(int i = 0; i < N; i++) hashV[disV[i]] += 1; 49 int **hashM = (int **) malloc(sizeof(int *)*(D+1)); 50 for(int d = 0; d <= D; d++) 51 hashM[d] = (int *) malloc(sizeof(int)*hashV[d]); 52 for(int d = 0; d <= D; d++) hashV[d] = 0; 53 for(int i = 0; i < N; i++){ 54 int d = disV[i]; hashM[d][hashV[d]++] = i; 55} 56 57 58 59 60 for(int d = 0; d <= D; d++){ 61 fprintf(fp, "d=%d (%d駅)\n", d, hashV[d]); 62 for(int j = 0; j < hashV[d]; j++) 63 fprintf(fp, "%s ", name[hashM[d][j]]); 64 fprintf(fp, "\n-----------------------\n"); 65 } 66 fclose(fp); 67}

queue.hの中身

C

1typedef struct{ 2 Node** nodes_ptr; 3 int first; 4 int last; 5} Queue; 6 7void init_queue(Queue *queue, Node n){ 8 queue->nodes_ptr = (Node **) calloc(n, sizeof(Node)); 9 queue->first = queue->last = 0; 10 11} 12 13void enq(Queue queue, int nodes_ptr){ 14 queue->arr[queue->last++] = input; 15} 16 17int deq(Queue *queue){ 18 nodes_ptr output = queue->arr[queue->first++]; 19 return output; 20 21}
エラーメッセージ In file included from cpgm/distance.c:4:0: cpgm/queue.h: In function ‘init_queue’: cpgm/queue.h:8:39: error: incompatible type for argument 1 of ‘calloc’ queue->nodes_ptr = (Node **) calloc(n, sizeof(N ^ In file included from cpgm/distance.c:2:0: /usr/include/stdlib.h:468:14: note: expected ‘size_t {aka long unsigned int}’ but argument is of type ‘Node {aka struct <anonymous>}’ extern void *calloc (size_t __nmemb, size_t __s ^ In file included from cpgm/distance.c:4:0: cpgm/queue.h: In function ‘enq’: cpgm/queue.h:14:8: error: invalid type argument of ‘->’ (have ‘Queue {aka struct <anonymous>}’) queue->arr[queue->last++] = input; ^ cpgm/queue.h:14:19: error: invalid type argument of ‘->’ (have ‘Queue {aka struct <anonymous>}’) queue->arr[queue->last++] = input; ^ cpgm/queue.h:14:31: error: ‘input’ undeclared (first use in this function) queue->arr[queue->last++] = input; ^ cpgm/queue.h:14:31: note: each undeclared identifier is reported only once for each function it appears in cpgm/queue.h: In function ‘deq’: cpgm/queue.h:18:3: error: unknown type name ‘nodes_ptr’ nodes_ptr output = queue->arr[queue->first++] ^ cpgm/queue.h:18:27: error: ‘Queue {aka struct <anonymous>}’ has no member named ‘arr’ nodes_ptr output = queue->arr[queue->first++] ^ cpgm/distance.c: In function ‘init_value’: cpgm/distance.c:15:22: error: incompatible type for argument 2 of ‘init_queue’ init_queue(&queue, N); ^ In file included from cpgm/distance.c:4:0: cpgm/queue.h:7:6: note: expected ‘Node {aka struct <anonymous>}’ but argument is of type ‘int’ void init_queue(Queue *queue, Node n){ ^ cpgm/distance.c: At top level: cpgm/distance.c:18:20: error: expected declaration specifiers or ‘...’ before ‘nodes_ptr’ void calc_distance(nodes_ptr origin){ ^ cpgm/distance.c: In function ‘print_distance’: cpgm/distance.c:63:30: error: subscripted value is neither array nor pointer nor vector fprintf(fp, "%s ", name[hashM[d][j]]); ^ cpgm/distance.c: In function ‘main’: cpgm/distance.c:74:14: warning: implicit declaration of function ‘calc_distance’ [-Wimplicit-function-declaration] int maxD = calc_distance(origin);

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

y_waiwai

2020/06/14 14:09

エラーメッセージを提示しよう
guest

回答1

0

void init_queue(Queue *queue, Node n){

queue->nodes_ptr = (Node **) calloc(n, sizeof(Node));

callocの第一引数はsize_tだけどそうなってませんな。
これをどうにかしよう

void enq(Queue queue, int nodes_ptr){

queue->arr[queue->last++] = input;
}

queue->なんたら、という表現はポインタじゃないとできません
これもどうにかしよう

そもそもの話になるけど、ヘッダファイルにコードは書くもんじゃありません。

投稿2020/06/14 14:42

編集2020/06/14 14:46
y_waiwai

総合スコア87774

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問