a simple question about typedef void* KEYWORK

J

jack

As a C beginner,I want to ask a "silly" question about typedef:

IS
typedef [something1] [something2]
means " someting1 is something2"

SUCH AS
typedef struct node{
int a;
int b;
} NODE;

show us NODE is struct node {...} ,when we met NODE, we should take
is as struct node{...} ,no problem.

but what about
typedef void* pthread_addr_t;
typedef void* (*pthread_startroutine_t) (void*);

when we met pthread_addr_t or (*pthread_startroutine_t) (void*)
we take it as void* ?
 
B

bartc

jack said:
As a C beginner,I want to ask a "silly" question about typedef:

IS
typedef [something1] [something2]
means " someting1 is something2"

SUCH AS
typedef struct node{
int a;
int b;
} NODE;

Yes, typedef is back-to-front like that. I can only remember it by
pretending typedef is an attribute like static, then the something2 (the new
name) is where the variable name goes.

And this means that sometimes, the something2 is *inside* something1, as in:

typedef int iarray[10];
but what about
typedef void* pthread_addr_t;
typedef void* (*pthread_startroutine_t) (void*);

when we met pthread_addr_t or (*pthread_startroutine_t) (void*)
we take it as void* ?

This is my static trick doesn't work as I haven't (and never will) master
complex type declarations in C.

(My own, I can deal with by decomposing into typedefs, but other
peoples'...)
 
T

Thomas Maier-Komor

jack said:
As a C beginner,I want to ask a "silly" question about typedef:

IS
typedef [something1] [something2]
means " someting1 is something2"

SUCH AS
typedef struct node{
int a;
int b;
} NODE;

show us NODE is struct node {...} ,when we met NODE, we should take
is as struct node{...} ,no problem.

but what about
typedef void* pthread_addr_t;
typedef void* (*pthread_startroutine_t) (void*);

when we met pthread_addr_t or (*pthread_startroutine_t) (void*)
we take it as void* ?

Think of void * typedefs as defining a pointer type to an opaque (i.e.
invisible) data structure.

Like this the implementation can easily be changed without having to
recompile everything. This kind of abstraction is of course only
possible, if the user doesn't need the ability to access the data
structure by himself.

In the case of pthread_attr_t and friends, special functions are
provided to modify the contents of the datastructure that is hidden by
the void *.

HTH,
Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,235
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top