.h에서 type def'd struct를 전달하는 방법
저는 Preprocessor.h를 가지고 있습니다.
#define MAX_FILES 15
struct Preprocessor {
FILE fileVector[MAX_FILES];
int currentFile;
};
typedef struct Preprocessor Prepro;
void Prepro_init(Prepro* p) {
(*p).currentFile = 0;
}
정의에서 선언을 분리해야 한다는 것을 그때 깨달았습니다.그래서 Preprocessor.c:
#define MAX_FILES 15
struct Preprocessor {
FILE fileVector[MAX_FILES];
int currentFile;
};
typedef struct Preprocessor Prepro;
그리고 Preprocessor.h는 지금:
void Prepro_init(Prepro* p) {
(*p).currentFile = 0;
}
그건 분명히 효과가 없어요 왜냐하면 교수님이..h는 Prepro 타입을 모릅니다.이미 여러 가지 조합을 시도해 봤지만 아무 것도 효과가 없었습니다.해결책을 찾을 수가 없습니다.
이동합니다.typedef struct Preprocessor Prepro;
파일과 c 파일의 정의를 머리글에 입력하고 Prepro_init 정의를 입력합니다.이것은 아무 문제없이 당신을 위해 그것을 앞으로 신고할 것입니다.
전처리기.h
#ifndef _PREPROCESSOR_H_
#define _PREPROCESSOR_H_
#define MAX_FILES 15
typedef struct Preprocessor Prepro;
void Prepro_init(Prepro* p);
#endif
전처리기
#include "Preprocessor.h"
#include <stdio.h>
struct Preprocessor {
FILE fileVector[MAX_FILES];
int currentFile;
};
void Prepro_init(Prepro* p) {
(*p).currentFile = 0;
}
다음의 정의를 숨기려면Preprocessor
, 이것을 헤더 파일에 간단히 넣을 수 있습니다.
struct Preprocessor;
typedef struct Preprocessor Prepro;
하지만 좀 더 일반적으로, 당신은 아마도 당신이 필요할 것입니다.Preprocessor
다른 코드가 실제로 사용할 수 있도록 헤더 파일의 정의입니다.
넣으셨습니다..c
무엇에 담길 것인가.h
,그리고 역도 성립.Prepro_init
꼭 있어야 할.c
파일, 그리고 그 파일은 반드시#include "Preprocessor.h"
.
YAS: 또 하나의 해결책.
전처리기.h
<some code>
void Prepro_init(Prepro* p) {
(*p).currentFile = 0;
}
<some code>
전처리기
#define MAX_FILES 15
struct Preprocessor {
FILE fileVector[MAX_FILES];
int currentFile;
};
typedef struct Preprocessor Prepro;
#include "Preprocessor.h" //include after defining your structure.
<some code>
{
struct Prepro p;
Prepro_init(p);
<some code>
.... using p.currentFile.....
.....using other members....
<some code>
}
<some code>
이제 될 겁니다.이것이 당신의 요구 사항이라고 생각합니다.도움이 되길 바랍니다.
단점:Preprocessor 구조의 멤버는 미리 결정되어야 합니다. 즉, 헤더 파일은 멤버 currentFile을 사용합니다.따라서 Prepro.h가 포함된 c 파일은 Prepro로 정의된 구조를 가져야 하며 해당 구조는 member currentFile.(이 경우)을 포함해야 합니다.
사용자 Avl 트리를 그래픽 트리 형식으로 표시하기 위해 헤더 파일을 작성하는 동안 1년 전에 겪었던 것과 같은 문제입니다.
Linus[1]을 따르고, type defstruct를 사용하지 않는 것이 좋습니다.
라이브러리를 제어하는 경우:
파일포함
struct Foo;
int foo_get_n(const struct Foo *bar);
구현파일중
struct Foo
{int n;};
int foo_get_n(const struct Foo *bar)
{return bar->n;}
라이브러리를 제어하지 않는 경우:
관리자에게 포함 파일에서 이러한 오염 유형의 결함을 제거해 달라고 요청합니다.
요약
type defstruct를 사용하지 마십시오.
[1] http://yarchive.net/comp/linux/typedefs.html
바꾸다.h
그리고..c
파일. 머리글 포함.c
.
또한 선언, 정의 및 헤더 파일이 무엇을 하는지에 대한 책을 참조하십시오.
언급URL : https://stackoverflow.com/questions/7259238/how-to-forward-typedefd-struct-in-h
'programing' 카테고리의 다른 글
AngularJS에서 쿼리 문자열 제거 (0) | 2023.10.24 |
---|---|
앵커(링크)의 밑줄을 제거하는 방법? (0) | 2023.10.24 |
자유 XML 포맷 도구 (0) | 2023.10.24 |
mariadb의 json(긴 텍스트) 필드에서 데이터 추출 (0) | 2023.10.24 |
클라이언트 통계 테이블(Microsoft SQL Server Management Studio) (0) | 2023.10.24 |