Xcode 警告我隐式声明 'malloc' 但 stdlib.h 包含在标头中

时间:2021-08-01 18:36:35

标签: xcode

我试图在 Xcode 中用 C 创建堆栈数据类型,因此使用堆栈初始化函数。

在 DeckStack.c 中找到以下内容

#define DS DeckStack

#include "DeckStack.h"


struct _DeckStack {
    void *top;
    void *cards[40];
};


DeckStack* stack_init(void) {
    DS *new_deckStack = NULL;

    new_deckStack = (DS*) malloc(sizeof(DS));
    
    return new_deckStack;
}

但 Xcode 抱怨 malloc 的隐式声明,而 stdlib.h 包含在 DeckStack.h 中:

#ifndef DeckStack_h
#define DeckStack_h

#include <stdio.h>
#include <stdlib.h>

typedef struct _DeckStack DeckStack;


/* @brief
 
* Creates a new deck
 
*/
DeckStack* stack_init(void);

#endif /* DeckStack_h */

1 个答案:

答案 0 :(得分:0)

解决了,只需按几次 Enter 键,.c 文件将使用标题的内容自行更新。