473,581 Members | 2,638 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

struct / typedef confusion

I just saw the following two lines in /usr/include/usb.h and my head is
spinning:

struct usb_dev_handle;
typedef struct usb_dev_handle usb_dev_handle;

What is that supposed to mean ?
I assume the first line is a forward declaration.
I can't find a definition for it, so does that mean it's like a void ?
Apparently gdb is confused as well as I can't seem to display anything based
on this struct.
--
Guillaume Dargaud
http://www.gdargaud.net/Antarctica/Penguins.html
May 29 '07 #1
8 8095
Guillaume Dargaud wrote:
I just saw the following two lines in /usr/include/usb.h and my head is
spinning:

struct usb_dev_handle;
typedef struct usb_dev_handle usb_dev_handle;
It means that both "struct usb_dev_handle" and the typedef
"usb_dev_handle " now refer to a struct with a tag of usb_dev_handle,
but you don't know what is inside that struct.

But it is enough information to declare for example a function
returning a pointer to a usb_dev_handle or taking one as an argument.
Like

usb_dev_handle* create_usb_dev_ handle (void);
void destroy_usb_dev _handle (usb_dev_handle * h);
size_t read_from_usb (usb_dev_handle * h, void* buffer, size_t size);

and now you can write code like

char buffer [10];
usb_dev_handle* p = create_usb_dev_ handle ();
read_from_usb (p, buffer, sizeof (buffer));
destroy_usb_dev _handle (p);

even though you don't have any idea what's inside the struct.

May 29 '07 #2
In article <f3**********@c cpntc8.in2p3.fr >,
Guillaume Dargaud <us************ *************** **@www.gdargaud .netwrote:
>I just saw the following two lines in /usr/include/usb.h and my head is
spinning:
>struct usb_dev_handle;
typedef struct usb_dev_handle usb_dev_handle;
>What is that supposed to mean ?
It means that struct usb_dev_handle is a structure of some kind,
whose contents are undefined to this section of code. "Incomplete "
declartions such as this are useful when writing library code in
which the internals of the defined object should be "opaque"
(known only to the implementation) .
>I assume the first line is a forward declaration.
I can't find a definition for it, so does that mean it's like a void ?
No, not like void, but a pointer to such a structure is akin to
void* -- a pointer to -something- that will be converted by the
library code to point to the real structure at need.
--
"law -- it's a commodity"
-- Andrew Ryan (The Globe and Mail, 2005/11/26)
May 29 '07 #3
It means that struct usb_dev_handle is a structure of some kind,
whose contents are undefined to this section of code. "Incomplete "
declartions such as this are useful when writing library code in
which the internals of the defined object should be "opaque"
(known only to the implementation) .
Thanks both of you. I didn't know this was possible in C, although I've
known for a long time that it isn't strongly typed.

Still, isn't there a contradiction in having both a struct and a typedef
with the same name ?
--
Guillaume Dargaud
http://www.gdargaud.net/Climbing/
May 29 '07 #4
On May 29, 3:22 pm, "Guillaume Dargaud"
<use_the_form_o n_my_contact_p. ..@www.gdargaud .netwrote:
I just saw the following two lines in /usr/include/usb.h and my head is
spinning:

struct usb_dev_handle;
typedef struct usb_dev_handle usb_dev_handle;

What is that supposed to mean ?
I assume the first line is a forward declaration.
I can't find a definition for it, so does that mean it's like a void ?
Apparently gdb is confused as well as I can't seem to display anything based
on this struct.
The line is declaring that a struct with a tag of usb_dev_handle
exists (which code would refer to as "struct usb_dev_handle" ). The
second line allows a shorter form to be used for it's name (just
"usb_dev_handle "). Because no information about what's in the struct
has been given, it's a so-called "incomplete type"; you can pass
pointers to it around, but not create an object of that type (at least
not safely, if you don't know how big it is). Normally, there'll be
some library function around that does know the details of the type
and which can create objects of that type; however, you have no way to
access the details yourself without that info, and neither does your
debugger (if you loaded debug information into it from the library,
assuming you have that information, it'll be able to display
information as it would with a complete type).

void is linked to this, in that it's an incomplete type that cannot be
completed, and so you can perform the same operations with it, but no
more:

struct usb_dev_handle;
typedef struct usb_dev_handle usb_dev_handle;

void* a; /* fine */
usb_dev_handle* b; /* fine */
void c; /* error */
usb_dev_handle d; /* error */

void func(void) /* void has a special meaning here that has nothing
to do with incomplete types */
{
usb_dev_handle e; /* error */
usb_dev_handle *f; /* fine */
void g; /* error */
void *h; /* fine */
h=malloc(10); /* legal, but probably a bad idea to use a magic
number here */
h=malloc(10*siz eof(void)); /* would be safer if it made sense, but
it doesn't */
f=malloc(10); /* legal but very stupid, as you have no idea how big
a
usb_dev_handle is */
f=malloc(10*siz eof(*f)); /* error, because the compiler has no idea
how big a
usb_dev_handle is either */
}

--
ais523

May 29 '07 #5
"Guillaume Dargaud" <us************ *************** **@www.gdargaud .net>
writes:
Still, isn't there a contradiction in having both a struct and a typedef
with the same name ?
It can be confusing to human readers to have a different
structure with the same struct tag and typedef, but it does not
confuse the compiler. Struct tags and typedefs are in different
namespaces: tags appear only immediately after the reserved word
"struct", and typedef names never do.

I don't think it's very confusing to give the same structure the
same struct tag and typedef name, but it's unnecessary. Usually,
in my code, I don't typedef my structs at all.
--
"Am I missing something?"
--Dan Pop
May 29 '07 #6
In article <f3**********@c cpntc8.in2p3.fr >,
Guillaume Dargaud <us************ *************** **@www.gdargaud .netwrote:
>Still, isn't there a contradiction in having both a struct and a typedef
with the same name ?
No, C has a few different "name spaces", with a usage in one
name space not conflicting with the usage in another name space.
For example, statement labels don't conflict with ordinary
identifiers. In 30 seconds reading of C89 I'm left uncertain
exactly which name space is used for typedefs, but the
tags of structures, unions, and enumerations are specifically
noted as being disambiguated by the presence of the
'struct', 'union' or 'enum' tag.
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
May 29 '07 #7
Ben Pfaff <bl*@cs.stanfor d.eduwrites:
"Guillaume Dargaud" <us************ *************** **@www.gdargaud .net>
writes:
>Still, isn't there a contradiction in having both a struct and a typedef
with the same name ?

It can be confusing to human readers to have a different
structure with the same struct tag and typedef, but it does not
confuse the compiler. Struct tags and typedefs are in different
namespaces: tags appear only immediately after the reserved word
"struct", and typedef names never do.

I don't think it's very confusing to give the same structure the
same struct tag and typedef name, but it's unnecessary. Usually,
in my code, I don't typedef my structs at all.
Neither do I in most cases, but this:

struct usb_dev_handle;
typedef struct usb_dev_handle usb_dev_handle;

is a case where using a typedef for a struct actually makes sense.
The type "usb_dev_handle " is an opaque type; the only thing client
code needs to know about it is that it's an object type, and you can
manipulate pointers to it (presumably using functions in the same
header that take arguments of type usb_dev_handle* ). Presumably
client code will never refer to members of the type, so it doesn't
need to know that it's a struct.

There's no real reason to use different identifiers for the struct tag
and the typedef name; using the same identifier makes it clearer that
they're really the same thing.

It's very similar to the handling of type FILE in <stdio.h>. It could
have been declared as "struct FILE", but using the simple identifier
"FILE" *deliberately* hides the internals. (Well, it doesn't
completely hide them, but it expresses the intent that they shouldn't
be used.)

On the other hand, if client code is intended to refer to members of
the structure, then adding a typedef merely creates as second name for
something that already has a perfectly good name, and purports to hide
information (the fact that the type is a struct) that really isn't
hidden at all.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
May 29 '07 #8
On Tue, 29 May 2007 15:51:54 +0000 (UTC), ro******@ibd.nr c-cnrc.gc.ca
(Walter Roberson) wrote:
In article <f3**********@c cpntc8.in2p3.fr >,
Guillaume Dargaud <us************ *************** **@www.gdargaud .netwrote:
Still, isn't there a contradiction in having both a struct and a typedef
with the same name ?

No, C has a few different "name spaces", with a usage in one
name space not conflicting with the usage in another name space.
Right.
For example, statement labels don't conflict with ordinary
identifiers. In 30 seconds reading of C89 I'm left uncertain
exactly which name space is used for typedefs, but the
The 'ordinary identifier' namespace, which also contains (named)
objects (aka variables), functions, and enum constants.
tags of structures, unions, and enumerations are specifically
noted as being disambiguated by the presence of the
'struct', 'union' or 'enum' tag.
- formerly david.thompson1 || achar(64) || worldnet.att.ne t
Jul 1 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
2603
by: Russell Shaw | last post by:
Hi, I have two structs in a header file, and they reference each other, causing a compile error. Is there a standard way to deal with this? typedef struct { ... RtAction *actions; } RtWidget;
5
542
by: Russell Shaw | last post by:
Hi, In setjmp.h on a linux system, there is: typedef struct __jmp_buf_tag { ... } jmp_buf; I could understand typedef struct { } jmp_buf, but how do i interpret typedef struct { } jmp_buf ?
15
2035
by: dutchgoldtony | last post by:
Hi all, I was just wondering if this is possible. I'm trying to implement a viterbi decoder in C and am creating an array of nodes (the struct), and an array of pointers to nodes (the member I'm worried about) connecting to it like so: // snippet start typedef struct _node{ char state; // The state of each node ("00","01","10","11")
20
2089
by: ma0001yu | last post by:
Hi, all. I feel confuse about below struct definition: typedef struct TAG { comments.... }; What my confusion is: is typedef extra??why we not just use
3
3202
by: Sudheer Gupta | last post by:
Hi, I am having trouble using C struct in python. Hope anyone can help me out ... Say, I have my C struct as typedef struct call { struct call *next;
8
28444
by: Mohammad Omer Nasir | last post by:
Hi, i made a structure in header file "commonstructs.h" is: typedef struct A { int i; A( ) {
15
2309
by: David Marsh | last post by:
Is there any functional difference (or any other reason to prefer one over the other) between these two methods: typedef struct mystruct { int a; int b; } mystruct; struct mystruct { int a;
21
1986
by: heavyz | last post by:
In C, if i want to declare a struct, there are 3 methods to do so: 1: struct dummy { ... }; 2: typedef struct { ... } dummy; 3: typedef struct _dummy { ... } dummy; Usually i use the first method to declare my structs, but in many open source projects (such as libxml2), the third method is used. I am just curious about the difference.....
7
18432
by: MJ_India | last post by:
Style 1: struct my_struct { ... }; typedef my_struct my_struct_t; Style 2: typedef struct my_struct {
0
7873
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8153
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8309
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6558
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5365
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3830
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2304
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1405
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1141
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.