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

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

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

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

コンパイルエラー

コンパイルのフェーズで生成されるエラーです。よく無効なシンタックスやタイプが含まれているとき発生します。

Q&A

解決済

1回答

1179閲覧

c言語 コンパイルエラー

tamintya

総合スコア34

C

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

コンパイルエラー

コンパイルのフェーズで生成されるエラーです。よく無効なシンタックスやタイプが含まれているとき発生します。

0グッド

0クリップ

投稿2021/05/26 13:35

編集2021/05/26 15:02

###c言語のコンパイルエラーを改善したいです

c言語の構造体リストを用いたキューを作りたいのですがコンパイル時にエラーが発生してしまいます。
何が原因でエラーが起きていて、どのように直せば良いのでしょうか教えて欲しいです。

発生している問題・エラーメッセージ

mondai2.c: In function ‘enq’: mondai2.c:27: error: request for member ‘next’ in something not a structure or union mondai2.c: In function ‘deq’: mondai2.c:38: error: request for member ‘num’ in something not a structure or union mondai2.c:39: error: lvalue required as left operand of assignment mondai2.c:40: error: request for member ‘next’ in something not a structure or union mondai2.c: In function ‘free_queue’: mondai2.c:66: error: lvalue required as left operand of assignment

該当のソースコード

c

1 2#include <stdio.h> 3#include <stdlib.h> 4#include<string.h> 5 6/* 構造体 cell の宣言 */ 7struct cell { 8 int num; 9 struct cell *next; 10}; 11typedef struct cell cell; 12 13/**********************/ 14/* enq 操作を行う関数 */ 15/**********************/ 16void enq(struct cell **head, struct cell **tail, int val) 17{ 18 cell *p; 19 p=(cell*)malloc(sizeof( cell )); 20 p->num = val; 21 p->next = NULL; 22 if(head == NULL){ 23 head = &p; 24 tail = &p; 25 } 26 else{ 27 tail->next = &p; 28 tail = &p; 29 } 30} 31 32/**********************/ 33/* deq 操作を行う関数 */ 34/**********************/ 35int deq(struct cell **head){ 36 cell *p; 37 int deqdata; 38 deqdata = head->num; 39 &p = head; 40 head = head->next; 41 free(p); 42 return deqdata; 43} 44 45/******************************/ 46/* キューの中身を表示する関数 */ 47/******************************/ 48void print_queue(struct cell *head) 49{ 50 cell *p = head; 51 while(p != NULL){ 52 printf(" -> %d" , p->num); 53 p=p->next; 54 } 55 printf("\n"); 56 57} 58 59/******************************/ 60/* キューの中身を空にする関数 */ 61/******************************/ 62void free_queue(struct cell **head) 63{ 64 cell *q; 65 cell *p; 66 &p = head; 67 while(p != NULL){ 68 q=p->next; 69 free(p); 70 p=q; 71 } 72 printf("プログラムを終了します\n"); 73} 74 75int main(void) 76{ 77 int indata; 78 char in[100]; 79 struct cell *listhead=NULL; /* リストの先頭を指すポインタ.データが無い場合はNULLとする. */ 80 struct cell *listtail; /* リストの末端を指すポインタ */ 81 while(1){ 82 printf("操作を入力してください(enq/deq/end) ==>"); 83 scanf("%s" , in); 84 //end入力時 85 if(strcmp(in , "end") == 0){ 86 free(&listhead); 87 } 88 89 //enq入力時 90 if(strcmp(in , "enq") == 0){ 91 printf("追加する値を入力して下さい ==>"); 92 scanf("%d" , &indata); 93 enq(&listhead , &listtail , indata); 94 print_queue(listhead); 95 if(listhead == NULL){ 96 listtail = NULL; 97 } 98 } 99 100 //deq入力時 101 if(strcmp(in , "deq") == 0){ 102 if(listhead == NULL){ 103 printf("キューにデータはありません\n"); 104 free_queue(&listhead); 105 break; 106 } 107 else{ 108 printf("%dはキューから消去されました\n" , deq(&listhead)); 109 print_queue(listhead); 110 } 111 112 } 113 114 } 115 116 117 return(0); 118} 119

(追記ソースコード)

#include <stdio.h> #include <stdlib.h> #include<string.h> /* 構造体 cell の宣言 */ struct cell { int num; struct cell *next; }; typedef struct cell cell; /**********************/ /* enq 操作を行う関数 */ /**********************/ void enq(struct cell **head, struct cell **tail, int val) { cell *p; p=(cell*)malloc(sizeof( cell )); p->num = val; p->next = NULL; if(head == NULL){ head = &p; tail = &p; } else{ (*tail)->next = &p; tail = &p; } } /**********************/ /* deq 操作を行う関数 */ /**********************/ int deq(struct cell **head){ cell *p; int deqdata; deqdata = (*head)->num; p = head; *head = (*head)->next; free(p); return deqdata; } /******************************/ /* キューの中身を表示する関数 */ /******************************/ void print_queue(struct cell *head) { cell *p = head; while(p != NULL){ printf(" -> %d" , p->num); p=p->next; } printf("\n"); } /******************************/ /* キューの中身を空にする関数 */ /******************************/ void free_queue(struct cell **head) { cell **q; cell **p; &p = head; while(p != NULL){ q=(*p)->next; free(p); p=q; } printf("プログラムを終了します\n"); } int main(void) { int indata; char in[100]; struct cell *listhead=NULL; /* リストの先頭を指すポインタ.データが無い場合はNULLとする. */ struct cell *listtail; /* リストの末端を指すポインタ */ while(1){ printf("操作を入力してください(enq/deq/end) ==>"); scanf("%s" , in); //end入力時 if(strcmp(in , "end") == 0){ free(&listhead); } //enq入力時 if(strcmp(in , "enq") == 0){ printf("追加する値を入力して下さい ==>"); scanf("%d" , &indata); enq(&listhead , &listtail , indata); print_queue(listhead); if(listhead == NULL){ listtail = NULL; } } //deq入力時 if(strcmp(in , "deq") == 0){ if(listhead == NULL){ printf("キューにデータはありません\n"); free_queue(&listhead); break; } else{ printf("%dはキューから消去されました\n" , deq(&listhead)); print_queue(listhead); } } } return(0); }

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

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

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

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

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

guest

回答1

0

ベストアンサー

tail->next(*tail).nextと同じですが、

struct cell **tail

ということは、(*tail)は構造体ではないのでエラーとなります。
*tailでは「構造体へのポインタ」であり、**tailこそ構造体(struct cell)になるわけですね。

投稿2021/05/26 13:42

itagagaki

総合スコア8402

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

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

tamintya

2021/05/26 14:56

回答ありがとうございます。 このエラーについて理解することができました。
tamintya

2021/05/26 15:02

このことを踏まえソースコードを書き直してみたのですがあっているでしょうか? また今度はfreeの方法が分かりません。 このことについても教えてほしいです。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問