Like Tree1Likes
  • 1 Post By grumpy

Structures nesting

This is a discussion on Structures nesting within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> int main() { struct address { char phone[15]; char city[25]; int pin; }; struct emp { char name[25]; ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    41

    Structures nesting

    Code:
    #include<stdio.h>
    int main()
    {
        struct address
        {
            char phone[15];
            char city[25];
            int pin;
        };
        struct emp
        {
            char name[25];
            struct address a;
        };
        struct emp e = {"jeru", "512455", "nagpur", 10};
        printf(" %s %s\n", e.name, e.a.phone);
        printf(" %s %d\n", e.a.city, e.a.pin);
        return 0;
    }
    In the above program how the below statement occur:
    Code:
    struct address a;
    Is that declared there ?
    And my compiler gives two warning :

    Code:
    gcc -Wall -o "untitled5" "untitled5.c" (in directory: /home/duh/Desktop/C)
    untitled5.c: In function ‘main’:
    untitled5.c:15:9: warning: missing braces around initializer [-Wmissing-braces]
    untitled5.c:15:9: warning: (near initialization for ‘e.a’) [-Wmissing-braces]
    Compilation finished successfully.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,662
    The "struct address a" is a definition (which is a particular type of declaration) of a struct address as a member of struct emp.

    To stop the compiler warning about missing braces, you need to add them in
    Code:
    struct emp e = {"jeru", {"512455", "nagpur", 10}};
    The internal set of braces contains the initialisers for the struct address. What you have isn't strictly an error, but is an approach that is easy to mess up when initialising complicated structures - which is why compilers often emit (or can be configured to emit) a warning about it.
    justine likes this.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Pelles C give no warnings, and runs the program fine, as it was in #1. Grumpy's version is more strictly correct, however.

    Unless the struct will ONLY be used in that function, it's much better to have the struct prototyped in global space, above main(), instead of inside it. Create the structs in main(), but define them outside it - that way every function can create local structs for their use, easily.
    Last edited by Adak; 11-26-2012 at 06:27 AM.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    41
    Thanks, that is clarified.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vector Nesting
    By CPlus in forum C++ Programming
    Replies: 10
    Last Post: 08-10-2010, 01:00 AM
  2. Nesting classes
    By DarkAlex in forum C++ Programming
    Replies: 4
    Last Post: 03-29-2009, 09:58 PM
  3. Mice Are Nesting!!!
    By Dagger in forum C++ Programming
    Replies: 4
    Last Post: 02-05-2006, 11:59 PM
  4. Nesting
    By coo_pal in forum C Programming
    Replies: 1
    Last Post: 01-27-2003, 11:16 PM
  5. Nesting Parentheses
    By XZSNPP in forum C++ Programming
    Replies: 6
    Last Post: 01-18-2003, 10:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21