0
typedef struct data * Data;

I have something like this that I don't understand. I have some function later on that returns Data. What I don't get is, data * Data seems to me like a "empty" struct .. It doesn't have anything in it.

So what do I create when I do something like Data d; and what do I returns from function if this struct is empty?

Thank you.

Razib
  • 10,965
  • 11
  • 53
  • 80
Gregory
  • 19
  • 1
  • 8
  • 1
    The actual problem is a bit difficult to tell from your description; could you please show some code calling the function in question? – Codor Mar 29 '15 at 18:30
  • Can you show the definition for `struct data`? It's hard to give you a sample use case of `Data` without knowing what exactly is `struct data`. – Filipe Gonçalves Mar 29 '15 at 18:42
  • what if you want a Data object that isn't a pointer? – Ryan Mar 29 '15 at 18:45
  • See [Is it a good idea to `typedef` pointers](http://stackoverflow.com/questions/750178), and [Hide type definition in C](http://stackoverflow.com/questions/9269691), and also [Does the C standard consider that there are one or two `struct uperms_entry` types in this header?](http://stackoverflow.com/questions/11697705). – Jonathan Leffler Mar 29 '15 at 19:53

2 Answers2

0

That line of code is creating a type alias. It means that Data is an alias for the type struct data *. So, whenever you see Data, it's as if you saw a pointer to struct data.

When you do something like Data d;, you're basically declaring a pointer to struct data. You're not initializing the pointer, so it doesn't point to a valid struct data. Dereferencing the pointer will thus be invalid.

When you have a variable of type Data, you need to point it to somewhere valid before trying to dereference it.

Perhaps a less confusing approach would have been to do it like this instead:

typedef struct data *Data_ptr;

Which at least would convey the idea that Data_ptr is a pointer type alias.

Filipe Gonçalves
  • 20,783
  • 6
  • 53
  • 70
0

You are looking at an opaque pointer.

Which means that the structure details are hidden to you, but the library functions that are using it are aware of the true content of the struct.

This type of declaration is usually made in public header files, while more private implementation use a complete declaration of the struct.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
dvhh
  • 4,724
  • 27
  • 33
  • Okay because in the project i have to do, teacher only gave use the headers, we have to implement it. Does that means that i have to "redefine" the structure in the .c file ? For using that structure? – Gregory Mar 29 '15 at 19:00
  • @dvhh: It's only _opaque_ if the `struct data` definition is not visible. Nothing in the question suggests it is hidden. – Johann Gerell Mar 29 '15 at 19:51
  • @CptDesro No. It usually means you will not need to directly manipulate the structure's fields. Assuming the teacher provided a library, those header files probably have the necessary function definitions that you can use to create and / or modify struct instances. Then you link to his library when you compile your project. But it's hard to tell if this is the case; you need to update your question with further information. Can't you show the header file? – Filipe Gonçalves Mar 29 '15 at 19:53