LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 04-14-2006, 08:37 PM   #1
angustia
Member
 
Registered: Apr 2003
Location: chile
Posts: 58

Rep: Reputation: 15
why C has two ways to declare structs?


hi, this is my doubt:

there are two ways:

struct { int a,b; } mystruct_s;

and

typedef struct { int a, b; } mystruct_t;

even there's a mixture:

typedef struct mystruct_s { int a,b; } mystruct_t;

is this something about early C standards or it has a purpose?

(the only thing i can guess is that it's so to declare the struct type in the header and the struct details in the .c file but it keeps looking weird...)

thanx.
 
Old 04-14-2006, 10:36 PM   #2
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
There are a lot of different ways to create a structure in C, and they're all actually a little bit different from one-another.

The basic way is to do something like this:
Code:
struct point {
    int x;
    int y;
};
struct point pt = {0, 0};
This creates pt as a struct of type "point", and sets the x and y members to zero.

Another way is using an anonymous struct:
Code:
struct {
    int x;
    int y;
} pt;
pt.x = 0;
pt.y = 0;
This method creates pt out of an anonymous struct, defined inside the pt declaration. In other words, it cuts out "struct point" part.

A more popular way uses typedef to create a new type:
Code:
typedef struct {
    int x;
    int y;
} point_t;
point_t pt;
pt.x = 0;
pt.y = 0;
This assigns the anonymous struct to the type point_t. By doing this you can treat point_t just like you would any other type to declare new point structs.

The problem with this last method is that members of the struct you are defining can't be of that type, because that type's definition isn't done yet. In other words, this is illegal:
Code:
typedef struct {
    node left;
    node right;
    int value;
} node;
Because node is used before it's defined. The solution, then is to forego the anonymous struct, and use a named one instead. So the following will suffice, for example for a binary tree:
Code:
typedef struct node {
    struct node left;
    struct node right;
    int value;
} node_t;
node_t parent_node;
node_t left_node;
node_t right_node;
parent_node.left = left_node;
parent_node.right = right_node;
And that's how you do trees in C.

Last edited by taylor_venable; 04-14-2006 at 10:38 PM.
 
Old 04-15-2006, 01:56 AM   #3
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
Quote:
Originally Posted by taylor_venable
Code:
typedef struct node {
    struct node left;
    struct node right;
    int value;
} node_t;
And that's how you do trees in C.
This won't work. The reason is that node has incomplete type at the point of declaration of left, i.e. the compiler doesn't know the size of node yet. Even worse (in the above code), this is a recursive composition, the compiler has to determine the size of node to determine the size of node.

The correct way is:
Code:
typedef struct node {
    struct node *left;
    struct node *right;
    int value;
} node_t;
The size of a pointer is independent of the referenced type, so the size of node is clear.
 
Old 04-15-2006, 03:42 AM   #4
EAD
Member
 
Registered: Mar 2006
Distribution: ARCH linux
Posts: 137

Rep: Reputation: 15
Tanx guys, nice info. I love the
Quote:
typedef struct node {
struct node *left;
struct node *right;
int value;
} node_t;
way, works nice.
 
Old 04-15-2006, 12:57 PM   #5
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
Quote:
Originally Posted by addy86
This won't work. The reason is that node has incomplete type at the point of declaration of left, i.e. the compiler doesn't know the size of node yet. Even worse (in the above code), this is a recursive composition, the compiler has to determine the size of node to determine the size of node.
Sorry about that; my mistake. In my hurry to point out the error from the typedef-ed node example, I ended up and made the same error myself. Just goes to show I should probably check my code more before I post it. Thanks for pointing that out.
 
Old 04-15-2006, 10:36 PM   #6
angustia
Member
 
Registered: Apr 2003
Location: chile
Posts: 58

Original Poster
Rep: Reputation: 15
typedef struct node {
struct node *left;
struct node *right;
int value;
} node_t;

well, it's a good explanation, but it still sounds weird to explain to a newcomer...
 
  


Reply



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
declare 16bit int C++ linuxmandrake Programming 3 02-05-2006 03:46 PM
declaration does not declare anything tristanm Programming 5 10-24-2005 04:00 PM
How to declare a defaul argument in my function? bigapple Programming 3 08-11-2005 10:27 PM
Help for perl--- Getopt::Declare arobinson74 Programming 1 08-20-2004 10:31 AM
How to declare constant strings in C/C++? Dark_Helmet Programming 3 05-29-2004 08:07 AM

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

All times are GMT -5. The time now is 01:57 AM.

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