Passing pointers to structs through functions

时间:2017-04-24 17:32:18

标签: c

Hi could you please help for an error in C? I have two source files and a header. The one source is the main and the other source is full of functions and the header contains the functions declarations This is the main.c

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

#include "fundeclarationheader.h"


int main() {
InfoSessionPtr  MySession=NULL;
My Session = Session;
function(MySession->Tab);

return 0;
}

and this is the other source file

InfoSessionPtr Session;
TabNodePtr Tab;

struct InfoSession {
TabNodePtr FirstTab;
TabNodePtr LastTab;
TabNodePtr CurrTab;
TabNodePtr AuxTab;
char*     OpeningAddress;
};

struct TabNode {
TabNodePtr PrevTab, NextTab;
SiteNodePtr FirstSite;
SiteNodePtr CurrSite;
};

struct SiteNode{
SiteNodePtr PrevSite, NextSite;
TabNodePtr UpTab;
char *Address;
};

void function(TabNodePtr CurrTab){
 //body of function
}

and now this is the header

typedef struct InfoSession *InfoSessionPtr;
typedef struct TabNode *TabNodePtr;
typedef struct SiteNode *SiteNodePtr;
void function(TabNodePtr);   

Well the problem here is that when trying to call the "function" in main I get a message with GCC : dereferencing pointer to incomplete type

3 个答案:

答案 0 :(得分:2)

How can the compiler know what an InfoSession struct contains when it compiles main.c? You need to put the struct definition in the header file, and then #include it in both source files.

答案 1 :(得分:0)

Try to combine you header and source file.

Struct need to be declared in header, before you do typedef. So If you can combine both this should be working fine.

答案 2 :(得分:0)

不能合并它们,因为它是一个练习巫婆需要封装另外如果我删除函数调用它可以顺利工作

另外

MySession = Session

行不正确,因为我更改了代码以便向您演示 这就是为什么在真正的propram中我使用一个存在于另一个源文件中的函数来返回Session,所以一切都很顺利 唯一的问题是带有“功能”调用的问题