Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

declaring a struct in .h file and implementing in .c file

Tags:

c

this is my .h file:

struct _MyString;  
typedef struct _MyString MyString;

i would like to declare its members in the .c file.

i tried:

typedef struct MyString{
    char * _str;// pointer to the matrix
    unsigned long _strLength;
    }MyString;

but it doesn't work. how do i declare the struct's memebers in the .c file?

thank you

like image 822
maayan Avatar asked Dec 07 '22 00:12

maayan


1 Answers

You need only 1 typedef. Keep the one you already have in the .h file and delete all others.

Now, the struct name is struct _MyString. That is what you should define in the .c file, not struct MyString: notice the absence of the '_'.

So

.h file

struct _MyString;  
typedef struct _MyString MyString;

.c file

#include "file.h"
struct _MyString {
    char * _str;// pointer to the matrix
    unsigned long _strLength;
};
like image 119
pmg Avatar answered Jan 28 '23 16:01

pmg