2012-02-17 294 views
0

我已經在這個問題上工作了大約一個星期,我查了很多「不兼容的指針類型」警告解決方案,但我仍然對如何解決這個編譯錯誤感到困惑。警告:不兼容的指針類型

我得到一個錯誤說:

char_stack_interface.c: In function ‘pop_char’: 
char_stack_interface.c:32: warning: passing argument 2 of ‘pop’ from incompatible pointer type 
char_stack_interface.c: In function ‘top_char’: 
char_stack_interface.c:43: warning: passing argument 2 of ‘top’ from incompatible pointer type 

這是我的代碼:

char_stack_interface.h:

#ifndef _CHAR_STACK_INTERFACE_H 
#define _CHAR_STACK_INTERFACE_H 

#include "stack.h" 

extern status push_char(stack *p_s, char c); 
extern status pop_char (stack *p_s, char *p_c); 
extern status top_char (stack *p_s, char *p_c); 

#endif 

stack.h:

#ifndef _STACK_H 
#define _STACK_H 

#include "globals.h" 

ABSTRACT_TYPE(stack); 

extern status init_stack (stack *p_S); 
extern bool empty_stack(stack *p_S); 
extern status push  (stack *p_S , generic_ptr data); 
extern status pop  (stack *p_S , generic_ptr *p_data); 
extern status top  (stack *p_S , generic_ptr *p_data); 

#endif 

char_stack_interface。 c:

#include <stdio.h> 
#include <stdlib.h> 
#include "char_stack_interface.h" 
#include "stack.h" 

status push_char(stack *p_s, char c) 
{ 
    char *p_c = NULL; 
    p_c = (char *) malloc(sizeof(char)); 

    if (p_c == NULL) 
     return ERROR; 

    *p_c = c; 

    if (push(p_s, p_c) == ERROR) { 
     free(p_c); 
     return ERROR; 
    } 

    return OK; 
} 
status pop_char (stack *p_s, char *p_c) 
{ 
    char *p_data; 

    if (pop(p_s, p_data) == ERROR) 
     return ERROR; 

    *p_c = *p_data; 

    free(p_data); 

    return OK; 
} 
status top_char (stack *p_s, char *p_c) 
{ 
    char *p_data; 

    if (top(p_s, &p_data) == ERROR) 
     return ERROR; 

    *p_c = *p_data; 

    return OK; 
} 
+3

您必須添加更多信息。更好地粘貼標題(或至少從那裏的主要類型定義)以及你得到警告的那一行。 – chetan 2012-02-17 16:55:02

+0

我剛剛意識到像一個假人,我沒有把我的錯誤信息在那裏對不起,我剛剛更新它:) – Cka91405 2012-02-17 16:55:48

+3

@ Cka91405:'流行'和'頂部'原型是什麼樣子? – netcoder 2012-02-17 16:58:43

回答

2

那麼無論generic_ptr類型是什麼,顯然編譯器不能自動將'char *'類型轉換爲泛型ptr類型。試着做你的第二個arg的一個明確的情況下,以流行和頂部如:

流行(P_S,(generic_ptr)P_DATA)

+0

我會試試! – Cka91405 2012-02-17 17:19:13

+0

就是這樣!哦,那真是太簡單了!謝謝你soo – Cka91405 2012-02-17 17:20:56

+1

不客氣。 – chetan 2012-02-17 18:10:53

2

假設generic_ptr是(因爲它通常是這種情況):

typedef void* generic_ptr; 

然後pop是:

extern status pop  (stack *p_S , void **p_data); 

而且你調用它像:

pop(stack*, char*); 

因此,您將char*參數傳遞給void**,該參數來自無效指針類型。根據如何在pop中處理指針,您必須將指針傳遞給指針並/或明確告訴編譯器如何通過顯式強制轉換來處理這種情況。

+0

看到我很困惑,因爲我假設從我的教科書中複製代碼並將其放入文件中,並且它沒有在書中顯示。這讓我更加困惑。 :( – Cka91405 2012-02-17 17:18:44

+0

@ Cka91405:它什麼都不顯示? – netcoder 2012-02-17 17:23:02

相關問題