C linked list
C linked list
This is a rather n00bish C question, but I generally program in C++. When you want to make a linked list with C, my compiler (DJGPP) won't allow me to do the following:
typedef struct
{
int somedata;
testStruct * next;
} testStruct;
Anyone know how to do this? I looked online, but only found C code which looks exactly like this...Currently I'm just calling them void *'s then casting them...which I really don't like.
typedef struct
{
int somedata;
testStruct * next;
} testStruct;
Anyone know how to do this? I looked online, but only found C code which looks exactly like this...Currently I'm just calling them void *'s then casting them...which I really don't like.
Re: C linked list
smbogan wrote:This is a rather n00bish C question, but I generally program in C++. When you want to make a linked list with C, my compiler (DJGPP) won't allow me to do the following:
typedef struct
{
int somedata;
testStruct * next;
} testStruct;
Anyone know how to do this? I looked online, but only found C code which looks exactly like this...Currently I'm just calling them void *'s then casting them...which I really don't like.
Code: Select all
typedef struct testStruct
{
int somedata;
struct testStruct * next;
} testStruct;
Code: Select all
struct testStruct
{
int somedata;
struct testStruct * next;
};
typedef struct testStruct testStruct;
- carbonBased
- Member
- Posts: 382
- Joined: Sat Nov 20, 2004 12:00 am
- Location: Wellesley, Ontario, Canada
- Contact:
Agreed. Any good coding style should differentiate the struct name from its typedef counterpart. I use:Solar wrote:...or you could do it even cleaner and not re-use the name of the struct (which gives me the creeps, style-wise).
Code: Select all
struct _TestStruct
{
int somedata;
struct _TestStruct *next;
} TestStruct;
I'd also recommend generalizing your lists and making the payload a void* pointer (which I generally typedef to a 'Pointer' type
![Smile :)](./images/smilies/icon_smile.gif)
You also could, conceivably, make lists of different typed objects -- but this would be bad
![Smile :)](./images/smilies/icon_smile.gif)
--Jeff
- Brynet-Inc
- Member
- Posts: 2426
- Joined: Tue Oct 17, 2006 9:29 pm
- Libera.chat IRC: brynet
- Location: Canada
- Contact:
Can say that my coding style uses lots and lots of typedefs, but none for structs or particular pointers and such. Functions pointers occasionally, when the code starts to look ugly. Most typedefs are just renames for common types to be named by their application so you don't swap f.ex. a physical page address with a virtual page address - that is, that the compiler warns you about that.Solar wrote:Actually, my coding style doesn't use typedef's at all except for function pointers.
- carbonBased
- Member
- Posts: 382
- Joined: Sat Nov 20, 2004 12:00 am
- Location: Wellesley, Ontario, Canada
- Contact:
Any reasoning behind it? Just curious.Seven11 wrote:I don't agree with you and I don't think that typedef should be sepparated from the struct definition (unless it has to)... but that's my personal coding style...
I have a lot of opaque pointers in my OS. The "user" would be using an opaque typedef to a pointer to a struct. The kernel itself, of course, uses the actual struct which is defined privately outside the header.
Because of this, I find it a convenient to have notation to differentiate the two.
How do you handle portability of types? Different platforms/compilers can have alternative bit sizes per data types, of course. I find a typedef the most convenient way to handle this.Solar wrote: Actually, my coding style doesn't use typedef's at all except for function pointers. Wink
--Jeff
- Combuster
- Member
- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
I think when he meant coding style, he meant anything beyond that what is required.carbonBased wrote:How do you handle portability of types? Different platforms/compilers can have alternative bit sizes per data types, of course. I find a typedef the most convenient way to handle this.Solar wrote: Actually, my coding style doesn't use typedef's at all except for function pointers. Wink
Otherwise........
Solar's PDCLIB wrote:Code: Select all
typedef signed int _PDCLIB_int32_t; typedef unsigned int _PDCLIB_uint32_t;
Code: Select all
typedef struct { int position; int parse_state; } _PDCLIB_fpos_t;
![Twisted Evil :twisted:](./images/smilies/icon_twisted.gif)
I personally use the typedef struct X {...} X; style. It works and people tend to think "structure!" when they see 'typedef struct'. I personally have to think twice when i come across a struct without typedef...
Most importantly, it works
![Smile :)](./images/smilies/icon_smile.gif)
ACK.Combuster wrote:I think when he meant coding style, he meant anything beyond that what is required.
The first two allow me to do the required typedefs (e.g., int32_t) in <stdint.h> to a configurable type (_PDCLIB_int32_t), i.e. keeping the configuration in one internal header, instead of spreading it out across all the standard headers.Otherwise........Solar's PDCLIB wrote:Code: Select all
typedef signed int _PDCLIB_int32_t; typedef unsigned int _PDCLIB_uint32_t;
Code: Select all
typedef struct { int position; int parse_state; } _PDCLIB_fpos_t;
The latter is a platform dependency; a system could handle file position with something other but a struct, and the typedef allows for that.
So, it's like you said - my code style is not to use typedefs, but I don't let my code style influence my design decisions... much.
![Cool 8)](./images/smilies/icon_cool.gif)
Every good solution is obvious once you've found it.
smbogan wrote:How do you not use typedefs for structs in C? I know how to do it in C++, but the same code for structs in C++ doesn't compile in DJGPP...
Code: Select all
struct MyLinkedList
{
int data;
struct MyLinkedList * next;
};
...
struct MyLinkedList * head;
...
They are "somewhat" OK for structs, but generally typedefs quickly start obfuscating what type a variable really is.
That can be bad enough if you have a type vector<Value>, which is typedef'ed to TableRow or ObjectList or ItemArray in various contexts, so you have to trace back through various headers to find out what members the type actually has.
It gets positively dreadful when people start typedef'ing pointer types, use standard names for non-standard constructs, or name containers after types they are not (maps that are named vector or vice versa, for example).
That can be bad enough if you have a type vector<Value>, which is typedef'ed to TableRow or ObjectList or ItemArray in various contexts, so you have to trace back through various headers to find out what members the type actually has.
It gets positively dreadful when people start typedef'ing pointer types, use standard names for non-standard constructs, or name containers after types they are not (maps that are named vector or vice versa, for example).
Every good solution is obvious once you've found it.