LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-27-2004, 01:05 PM   #1
MadCactus
Member
 
Registered: Jul 2003
Distribution: Slackware 9.1
Posts: 195

Rep: Reputation: 30
Self referential structs in C?


Hi,

I want to do something like:

Code:
struct thing {
    thing *ptr;
};
But gcc won't let me do it. I can use g++ to compile, but is there a way to do this in C?
 
Old 01-27-2004, 01:07 PM   #2
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
it shold be

struct thing{
struct thing *ptr;
}
 
Old 01-27-2004, 01:14 PM   #3
wapcaplet
LQ Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
I don't know if it applies to structs, but sometimes with classes, you need to do a forward declaration - that is, tell the compiler "I'm gonna have a class called Thing, so don't freak out if you find references to Thing before Thing is defined." It looks like:

class Thing; // forward declaration

class Thing
{
Thing * ptrThing;
};

Sort of the same thing you do with functions; if a function calls another function that isn't defined until later on, it helps to put in a forward declaration for it.
 
Old 01-27-2004, 01:24 PM   #4
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
I believe you can do that, but if you declare it like infamous mentioned there is no need.
 
Old 01-27-2004, 01:34 PM   #5
cjcuk
Member
 
Registered: Dec 2003
Distribution: Openwall, ~LFS
Posts: 128

Rep: Reputation: 15
The reason, I believe, that it works with G++ and not GCC is due to C++ making a new `type' out of a struct creation, whereas C does not. If you want to refer to it as `Thing' after, use a typedef.
 
Old 01-27-2004, 02:29 PM   #6
MadCactus
Member
 
Registered: Jul 2003
Distribution: Slackware 9.1
Posts: 195

Original Poster
Rep: Reputation: 30
Doesn't seem to work either with "struct" or a forward declaration and changing the notation (typedef struct) also has no effect... I'm sure there must be a way to do it though.
 
Old 01-27-2004, 03:21 PM   #7
wapcaplet
LQ Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
Dunno. Works for me:

Code:
struct thing
{
  struct thing * aThing;
};
typedef struct thing thing;
What errors do you get when you try to do it?
 
Old 01-27-2004, 03:33 PM   #8
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Ya, I have to say, wapcaplet's method works great for me, I have a ton of source code I have written that uses it and I don't have a problem getting any of it to compile.....
 
Old 01-27-2004, 05:13 PM   #9
MadCactus
Member
 
Registered: Jul 2003
Distribution: Slackware 9.1
Posts: 195

Original Poster
Rep: Reputation: 30
Hi, beleive me i'm fairly sure that doesn't work on my system (gcc 3.2, kernel 2.4).
The errors I get are an initial "parse error" at the struct, and then a host of parse errors and variable undeclared errors wherever I use that type.

The code in question:

Code:
typedef struct _list_element_t {
    int val;
    _list_element_t *next;
};

typedef _list_element_t list_element_t;
Now this compiles with g++ no problems, i'm truly puzzled...
 
Old 01-27-2004, 05:34 PM   #10
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
as wapcaplet said above you need

by wapcaplet
Code:
struct thing
{
  struct thing * aThing;
};
typedef struct thing thing;


so putting this in the context of your code it should be

Code:
typedef struct _list_element_t {
    int val;
    struct _list_element_t *next;
};

typedef _list_element_t list_element_t;
 
Old 01-28-2004, 08:43 AM   #11
MadCactus
Member
 
Registered: Jul 2003
Distribution: Slackware 9.1
Posts: 195

Original Poster
Rep: Reputation: 30
Hey I tried that and it only threw up an additional error message... What version of gcc are you guys using?
 
Old 01-28-2004, 08:52 AM   #12
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Here, I pulled this straight out of some old source code of mine. Compiles under gcc 3.2.3 for sure, but I know it worked a long time ago when I wrote it as well..

struct _node {
int data;
struct _node *next;
};

typedef struct _node node;
 
Old 01-28-2004, 10:02 AM   #13
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Quote:
Originally posted by kev82

so putting this in the context of your code it should be

Code:
typedef struct _list_element_t {
    int val;
    struct _list_element_t *next;
};

typedef _list_element_t list_element_t;
Almost. Remove the first typedef, add another struct in the first typedef.

Code:
struct _list_element_t {
    int val;
    struct _list_element_t *next;
};

typedef struct _list_element_t list_element_t;
Or, keep the first typedef, add what you are typedeffing it to, and remove the second typedef

Code:
typedef struct _list_element_t {
    int val;
    struct _list_element_t *next;
} _list_element_t;
Both of these work fine for me with gcc 3.3.1

Last edited by deiussum; 01-28-2004 at 10:07 AM.
 
Old 01-28-2004, 11:16 AM   #14
MadCactus
Member
 
Registered: Jul 2003
Distribution: Slackware 9.1
Posts: 195

Original Poster
Rep: Reputation: 30
Thanks for that knowing peoples, I was being lazy with my structs as you might have guessed. Was another one that was causing the problem.
 
Old 01-28-2004, 06:29 PM   #15
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
oops, i wasnt really paying attention i just copied MadCactus' code and added the struct keyword, i didnt notice there were two typedefs.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
copy structs in c alaios Programming 10 09-10-2005 03:31 PM
calling global structs LuderForChrist Programming 8 08-11-2004 01:42 AM
pointers to structs in C spuzzzzzzz Programming 5 06-03-2004 06:41 PM
static structs? simbo Programming 3 02-05-2004 05:00 AM
structs and allocation of mem h/w Programming 4 12-22-2003 01:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration