Thread: structures

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    20

    structures

    is:

    Code:
    struct student {
    char id[10];
    char name[26];
    int gradePoints;
    };
    the same as:
    Code:
    struct {
    char id[10];
    char name[26];
    int gradePoints;
    } student;

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    Nope.

    The first declares a type named student, the second declares an object named student.

    Interestingly, you could combine them like this:

    Code:
    struct student {
    char id[10];
    char name[26];
    int gradePoints;
    } student;

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There are conceivably [at least] four variants:
    Code:
    struct student {
    char id[10];
    char name[26];
    int gradePoints;
    };
    The above defines a type called "struct student". You need to use "struct student <name>" to create a variable <name>, which can then be used to store the id, name and gradepoints.

    Code:
    struct {
    char id[10];
    char name[26];
    int gradePoints;
    } student;
    Declares a variable called student, with an unnamed struct - this means that the struct can not be used to create any further variables than the statement it is in. This is very rarely useful, but there are situations where it can be useful.

    Code:
    struct student {
    char id[10];
    char name[26];
    int gradePoints;
    } student;
    Declares a struct student type, then a variable called student. You can create further instances just like in the first variant. [1]

    Code:
    typedef struct student {
    char id[10];
    char name[26];
    int gradePoints;
    } student;
    This declares a struct student, then declares a type of the name student as an "alias" name for that struct, so you can create a new variable struct student <name> by "student <name>".

    [1] Note that in C++, an implicit typedef is made like the last example, and by using the same struct name and variable name, you are effectively hiding the implicit typedef. You can still use the C style "struct student <name>" to create further instances.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Structures within Structures
    By Misko82 in forum C Programming
    Replies: 2
    Last Post: 08-27-2007, 12:25 AM
  3. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  4. Structures, and pointers to structures
    By iloveitaly in forum C Programming
    Replies: 4
    Last Post: 03-30-2005, 06:31 PM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM