Thread: struct

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    23

    struct

    hello again,
    In books that I have read, the way to declare a struct is as shown in (a)
    However, I have seen code which is shown in (b)


    a)
    struct node{
    int number;
    };

    and this

    b)
    struct {
    int number;
    }node;

    note that 'node' is in front in (a) and 'node' is at the back in (b)

    Are they the same? what's the purpose of declaring a struct as shown in (b)?

    thanks

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    95
    personally i use the example from B, however i would do something like:
    Code:
    struct node
    { 
      int number; 
    } NODE;
    the advantage of putting the node bit at the end is that to create a new variable using this structure, you can simple do:
    Code:
    NODE newvariable;
    newvariable.number=5;
    basically they both do the same thing, if youre doing a linked list you'll definately have to do it the same way as a but can also do it like the way above which is what i use. Hope this clarifies things.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    What the second example might be inting at is the ability for you to decalre some instances of the struct inline, without having to declare them in a seperate instruction.

  4. #4
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    Posts
    195
    typedef struct node {
    int number;
    } NODE;
    'During my service in the United States Congress, I took the initiative in creating the Internet.' - Al Gore, March 9, 1999: On CNN's Late Edition

  5. #5
    Registered User breed's Avatar
    Join Date
    Oct 2001
    Posts
    91
    As stautze has pointed out but without any info.
    Typedef will define the structure as a type, like char, or int, or float, so when you would declare it , it would look like

    NODE newtype;

    instead of having to type

    struct NODE new type;
    and assigning values would be the same

    NODE.number=5;
    Before you judge a man, walk a mile in his
    shoes. After that, who cares.. He's a mile away and you've got
    his shoes.
    ************William Connoly

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  4. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  5. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM