By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,197 Members | 1,057 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,197 IT Pros & Developers. It's quick & easy.

Purpose of variableless struct

100+
P: 1,059
Lets take a look at the below declaration

Expand|Select|Wrap|Line Numbers
  1. struct strange_struct;
  2.  
In the above structure declaration no variable is added. Anyway it don't give any compilation error.

On the other hand if I tried to add the below line
Expand|Select|Wrap|Line Numbers
  1. printf("%d\n",sizeof(struct strange_struct));
It creates a compilation error
Expand|Select|Wrap|Line Numbers
  1. use of undefined type 'strange_struct'
What is the purpose of this kind of definiation.
Sep 9 '10 #1

✓ answered by Oralloy

Think about what a void pointer is for a moment....

If I give you a void * value, what is it really pointing to?

C and C++ are typed languages. Isn't it better if I give you an IOBuffer * in preference to a void * that you have to cast?

If you want, you can always just pass everything around as void * pointers and use byte offsets to get what you need from complex structures. On the other hand, will you reall get much done that way.

In other words, use C/C++ typing to your advantage.

Structures are abstractions that represent some underlying physical structure that you really don't want to know about most of the time. You just want the bits with convienent names attached.

The value of forward declarations is that they allow you to build header files that are independent of the structure body. This is a good way to hide data that the end-user does not really need access to. Especially if you build a comprehensive library/API to access this wonder struct that you've built for the user.

Another way to explain it is with "Standard I/O". We all know what a FILE * is, right? But what is the real beast underneath? It varies depending on the system implementation. However, they could have just as easily used void *. But would the function declarations have been as clear or meaningful when you read through the files? Would the compiler catch problems like:
Expand|Select|Wrap|Line Numbers
  1. fprintf("%.10s%2d", myString, myInt);
Anyway, its about time to fall off my high horse.

Good Luck!

Share this Question
Share on Google+
5 Replies


Oralloy
Expert 100+
P: 983
The struct definition you wrote is actually a forward declaration.

Basically it says that strange_struct is a structure, and that nothing is known about its details.

It allows you to write things like function declarations that depend on the existance of the structure, but that do not need to have the details of the structure. For example, this declaration needs to know that strange_struct exists, but it does not need to know details of the structure. What it does need to know is that the address of such a structure is a legitimate entity within the compilation:
Expand|Select|Wrap|Line Numbers
  1. struct strange_struct *MakeNew_strange_struct(void);
If you really want a zero length structure, you will have to use code similar to this:
Expand|Select|Wrap|Line Numbers
  1. struct strange_struct 
  2. {
  3.   // empty
  4. };
Hopefully that answers your question. If not, let me know and we can discuss it further.

Cheers!
Sep 9 '10 #2

100+
P: 1,059
That explain a lot.

But still, if I can get a real example it may help even more.

Though I have got some use in linux kernel source, but not clear about it yet.

I can think of a pointer to hold address but I can do this with void pointer too..

or totally confused........
Sep 9 '10 #3

Oralloy
Expert 100+
P: 983
Think about what a void pointer is for a moment....

If I give you a void * value, what is it really pointing to?

C and C++ are typed languages. Isn't it better if I give you an IOBuffer * in preference to a void * that you have to cast?

If you want, you can always just pass everything around as void * pointers and use byte offsets to get what you need from complex structures. On the other hand, will you reall get much done that way.

In other words, use C/C++ typing to your advantage.

Structures are abstractions that represent some underlying physical structure that you really don't want to know about most of the time. You just want the bits with convienent names attached.

The value of forward declarations is that they allow you to build header files that are independent of the structure body. This is a good way to hide data that the end-user does not really need access to. Especially if you build a comprehensive library/API to access this wonder struct that you've built for the user.

Another way to explain it is with "Standard I/O". We all know what a FILE * is, right? But what is the real beast underneath? It varies depending on the system implementation. However, they could have just as easily used void *. But would the function declarations have been as clear or meaningful when you read through the files? Would the compiler catch problems like:
Expand|Select|Wrap|Line Numbers
  1. fprintf("%.10s%2d", myString, myInt);
Anyway, its about time to fall off my high horse.

Good Luck!
Sep 9 '10 #4

100+
P: 1,059
Thats make a lot of sense :) thanks for your reply.
Sep 9 '10 #5

100+
P: 1,059
In fact this question came to my mind while I was reading
sock.h header file. While I was searching for definition for sock I got like this
Expand|Select|Wrap|Line Numbers
  1. struct sock;
Then I stopped searching. In fact if I have continue searching I may would get the correct answer. Cause it was just declaration but the definition was little later in the same file.

:)
Sep 16 '10 #6

Post your reply

Sign in to post your reply or Sign up for a free account.