Thread: struct definition

  1. #1
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501

    struct definition

    I am relatively new to c, and I get structures and how they work, but in my c book it says to declare them like this

    struct name {
    type name;
    } variable

    or you can do this

    struct name {
    type name;
    }
    struct name variable;
    I saw the other day that you don't need to give the structure a name, you can just do this
    struct {
    type name;
    } variable

    they both work, can someone explain this to me, why is there two ways?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    The first two create a named structure. You can make multiple variables of a named structure throughout your program. The last creates an unnamed structure, you can only create variables of that structure at the structure declaration:
    Code:
    struct A {
        ...
    } var1; /* Good */
    
    ...
    
    struct A var2; /* Good */
    Code:
    struct {
        ...
    } var; /* Good */
    
    /* You can't make any more variables of the unnamed structure. */
    My best code is written with the delete key.

  3. #3
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    thanx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 11:10 PM
  2. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM