C linked list

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
smbogan
Member
Member
Posts: 29
Joined: Tue Nov 21, 2006 3:17 pm

C linked list

Post by smbogan »

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.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re: C linked list

Post by Candy »

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;
Should work. The name testStruct isn't defined until after the struct definition, while the name "struct testStruct" is at the start. You can also choose a slightly cleaner definition:

Code: Select all

struct testStruct
{
   int somedata;
   struct testStruct * next;
};
typedef struct testStruct testStruct;
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post by Solar »

...or you could do it even cleaner and not re-use the name of the struct (which gives me the creeps, style-wise). 8)
Every good solution is obvious once you've found it.
User avatar
carbonBased
Member
Member
Posts: 382
Joined: Sat Nov 20, 2004 12:00 am
Location: Wellesley, Ontario, Canada
Contact:

Post by carbonBased »

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). 8)
Agreed. Any good coding style should differentiate the struct name from its typedef counterpart. I use:

Code: Select all

struct _TestStruct
{
   int somedata;
   struct _TestStruct *next;
} TestStruct;
Although I understand why some find underscores ugly, and may not like *my* specific approach.

I'd also recommend generalizing your lists and making the payload a void* pointer (which I generally typedef to a 'Pointer' type :)). This way the same routines can be used to create lists of ints, strings, objects, etc.

You also could, conceivably, make lists of different typed objects -- but this would be bad :) And, obv. is the downfall of void pointers.

--Jeff
Seven11
Member
Member
Posts: 25
Joined: Mon Oct 30, 2006 12:48 pm

Post by Seven11 »

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...
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post by Solar »

Actually, my coding style doesn't use typedef's at all except for function pointers. ;-)
Every good solution is obvious once you've found it.
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post by Brynet-Inc »

I ran into such a thing when attempting to port a friends application to OpenBSD, Such structures are annoying and confusing.
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Post by Candy »

Solar wrote:Actually, my coding style doesn't use typedef's at all except for function pointers. ;-)
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.
smbogan
Member
Member
Posts: 29
Joined: Tue Nov 21, 2006 3:17 pm

Post by smbogan »

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...but, the approaches he gave me worked fine.

Personally, my coding style doesn't really care, so long as it works...
User avatar
carbonBased
Member
Member
Posts: 382
Joined: Sat Nov 20, 2004 12:00 am
Location: Wellesley, Ontario, Canada
Contact:

Post by carbonBased »

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...
Any reasoning behind it? Just curious.

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.
Solar wrote: Actually, my coding style doesn't use typedef's at all except for function pointers. Wink
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.

--Jeff
User avatar
Combuster
Member
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:

Post by Combuster »

carbonBased wrote:
Solar wrote: Actually, my coding style doesn't use typedef's at all except for function pointers. Wink
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.
I think when he meant coding style, he meant anything beyond that what is required.

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:

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 :)
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
elderK
Member
Member
Posts: 190
Joined: Mon Dec 11, 2006 10:54 am
Location: Dunedin, New Zealand
Contact:

Post by elderK »

could go :

typedef struct def_bla bla;

struct def_bla {
bla* next;
...whatever else...
};

bla* head;
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post by Solar »

Combuster wrote:I think when he meant coding style, he meant anything beyond that what is required.
ACK.
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:
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.

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. 8)
Every good solution is obvious once you've found it.
Jules
Member
Member
Posts: 30
Joined: Mon Jan 08, 2007 3:19 am
Location: UK

Post by Jules »

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;

...
The only difference from C++ is that you need to precede references to the structure's name with the keywork 'struct'. IMO, this is a *lot* cleaner than using typedefs all the time.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post by Solar »

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).
Every good solution is obvious once you've found it.
Post Reply