2017-01-15 4 views
-2

Greg Perry 및 Dean Miller의 C 프로그래밍 절대 초보자 용 안내서컴파일러가 왜 "호환되지 않는 포인터 유형"경고를 표시합니까?

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를 다시 사용하는 법을 배웁니다. 제 게시물이 커뮤니티 가이드 라인을 따르지 않는다면 친절하게 알려주십시오.

+7

'bookinfo'! ='bookInfo' –

+1

__이 질문은 더 이상 재생 될 수 없거나 간단한 인쇄 오류로 인해 발생한 것입니다. 비슷한 질문이 여기에 주제 일지 모르지만, 이것은 미래 독자를 돕지 않을 방법으로 해결되었습니다. 이것은 게시하기 전에 문제를 재현하는 데 필요한 가장 짧은 프로그램을 확인하고 면밀히 검사함으로써 __ 피할 수 있습니다 __ –

답변

7

컴파일러는 대소 문자를 구분하므로 bookinfobookInfo은 다른 것입니다.