-2
Greg PerryとDean MillerのC Programming Absolute Beginner's Guideコンパイラが「互換性のないポインタ型」警告を表示するのはなぜですか?
サブトピックの第27章では、構造変数にデータを入れると次のコードが提供されています。
コードを実行しようとすると、かなりのエラーが発生します。最初のものは
27ex2.c:23:14: warning: incompatible pointer types assigning to
'struct bookinfo *' from 'struct bookInfo *'
[-Wincompatible-pointer-types]
books[ctr] = (struct bookInfo*)malloc(sizeof(struct bookInfo));
ここにコードがあります。
#include "bookinfo.h"
#include <stdio.h>
#include <stdlib.h>
int main()
{
int ctr;
struct bookinfo * books[3]; // Array of three structure variables
// Get the information about each book from the user
for (ctr = 0; ctr < 3; ctr++)
{
books[ctr] = (struct bookInfo*)malloc(sizeof(struct bookInfo));
printf("What is the name of the book #%d?\n", (ctr+1));
gets(books[ctr]->title);
puts("Who is the author? ");
gets(books[ctr]->author);
puts("How much did the book cost? ");
scanf(" $%f", &books[ctr]->price);
puts("How many pages in the book? ");
scanf(" %d", &books[ctr]->pages);
getchar(); //Clears newline input to keep things clean for
// next round
}
// Print a header line and then loop through and print the info
printf("\n\nHere is the collection of books:\n");
for (ctr = 0; ctr < 3; ctr++)
{
printf("#%d: %s by %s", (ctr+1), books[ctr]->title, books[ctr]->author);
printf("\nIt is %d pages and costs $%.2f", books[ctr]->pages, books[ctr]->price);
printf("\n\n");
}
return(0);
}
なぜこのエラーが発生しているのですか?
これが必要な場合のためのヘッダーファイルです。
// This header file defines a structure for information about a book
struct bookInfo {
char title[40];
char author[25];
float price;
int pages;
};
ご協力いただきありがとうございます。私はstackoverflowをもう一度使用することを学んでいますので、私の投稿がコミュニティのガイドラインに従わない場合は親切に教えてください。
'bookinfo'!=' bookInfo' –
__この質問は、もはや再現できない問題や単純な誤字によって生じたものです。同様の質問がここでは話題になるかもしれないが、これは将来の読者を助けるとは思わない方法で解決された。これは、投稿する前に問題を再現するのに必要な最短のプログラムを特定して綿密に検査することで回避できます –