0

i read many threads like How to make struct members private? Hiding members in a C struct but i didn't help me, such that i have a header file

#ifndef TEST_H_
#define TEST_H_
typedef struct point point;
#endif /* TEST_H_ */

and c file

#include "test.h"
struct point
{
    void *data;
};

and when i'm trying to create an instance of point structure in the main.c as

static point objpoint;
main()
{
}

the compiler is getting this error Description Resource Path Location Type

237 variable "objpoint" was declared with a never-completed type main.c

BTW, if i defined a pointer to the struct like

static point *ppoint;

the compiler won't generate any errors

Also, one more important information that i need to avoid any dynamic allocation for the struct object

please advise.

Community
  • 1
  • 1
Mohamed Fawzy
  • 43
  • 1
  • 6
  • Did you actually define `TEST_H_`? – Sourav Ghosh May 24 '16 at 21:59
  • Structs do not work like functions - structs must be defined in every translation unit that uses them. – user253751 May 24 '16 at 22:01
  • 1
    `main()` without a return type is very old and invalid now. – Iharob Al Asimi May 24 '16 at 22:01
  • yes i defined TEST_H_ as i show in the code – Mohamed Fawzy May 24 '16 at 22:05
  • the main is containing code but i didn't like to make the code area crowded – Mohamed Fawzy May 24 '16 at 22:05
  • in the C language, there is no concept of `private`, However, you can perform data hiding using the technique shown in @R Sahu's answer. Except, say nothing about the names/content of the struct in the *.h file. Place the instance of the struct (with the `static` modifier) inside test.c, along with all the functions to manipulate that struct. Any file that wants to use that struct includes the test.h header file. – user3629249 May 25 '16 at 19:56
  • Note: returning pointer to the actual instance of the struct avails nothing. Don't do that. Only allow access to an `interface` to the struct. Then the user can only access the struct through the interface and need know nothing about the actual struct contents/layout/location/etc. – user3629249 May 25 '16 at 20:01

2 Answers2

2

You cannot create an object of type struct point with just a forward declaration.

You need to add functions to construct instances of struct point and manipulate its data. In these functions, you deal only with pointers.

Add the declarations in the .h file:

#ifndef TEST_H_
#define TEST_H_

typedef struct point point;

point* constructPoint();
void setX(point* p, int x);
void setY(point* p, int y);

int getX(point* p);
int getY(point* p);


#endif /* TEST_H_ */

Define the struct and the functions in the .c file:

#include "test.h"
struct point
{
   int x;
   int y;
};

point* constructPoint()
{
   return calloc(1, sizeof(point));
}

void setX(point* p, int x)
{
   p->x = x;
}

void setY(point* p, int y)
{
   p->y = x;
}

int getX(point* p)
{
   return p->x;
}

int getY(point* p)
{
   return p->y;
}

Use the functions to construct and manipulate the object in main.c:

#include "test.h"

int main()
{
   point* pt_ptr = constructPoint();
   setX(pt_ptr, 10);
   printf("%d\n", getX(pt_ptr));
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • My problem that i want to avoid any dynamic allocation, so i need to allocate the structure statically – Mohamed Fawzy May 24 '16 at 22:07
  • @MohamedFawzy, is there a fixed number of such objects? If not, dynamic allocation is probably your only option. – R Sahu May 24 '16 at 22:09
  • one object will be created in the whole application – Mohamed Fawzy May 24 '16 at 22:11
  • 1
    Then you can just use create a function called `point* getPoint()` and make sure that that function returns a pointer to an object that is allocated statically. – R Sahu May 24 '16 at 22:12
  • 1
    @Mohamed Fawzy It is not considerate to add requirements like "avoid any dynamic allocation" in a comment here, yet leave the info put of the post. Add it to the post if it is important. – chux - Reinstate Monica May 24 '16 at 22:19
0

Well, you have already answered your question. If you are trying to hide structure members from other code, you have to use a pointer.

The issue is, when compiler encounters this line in your code:

static point objpoint;

it needs to know how much space to allocate for it and for that it needs to know what are the data members of the structure. So, it has to be fully defined at this point in code with all the fields and their types.

If you want to hide data members of the structure. You need to define a creation function returning a pointer to your structure (as noted in another answer). And only use pointers to your structure everywhere else.

You can allocate a structure statically, but only in the file where the structure is defined. Then you can return a pointer to that variable instead of allocating a new one. But you still have to use a pointer to reference to that structure everywhere else.

Seva
  • 2,388
  • 10
  • 9