Thread: struct question

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    12

    struct question

    Code:
    #include<stdio.h>
    struct game
    {
    int level;
    int score;
    struct player
    {
    char *name;
    }g2={"anil"};
    }g3={10,200};
    void main()
    {
    struct game g1=g3;
    clrscr();
    printf("%d  %d  %s",g1.level,g1.score,g1.g2.name);
    getch();
    }
    
    output:
    Line 9: error: expected ':', ',', ';', '}' or '__attribute__' before '=' token
    In function 'main':
    Line 15: error: 'struct game' has no member named 'g2'
    


    What is the reason behind the error messages?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    The resason for the error messages is that you are typing in garbage code...

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    The initialiser for your struct would be
    Code:
    struct game
    {
    int level;
    int score;
    struct player
    {
    char *name;
    }g2;
    }g3 = { 100, 200, { "hello" } };
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    12
    Thanks Salem...That helped

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-23-2011, 09:00 AM
  2. Struct delcaring a struct within itself question
    By illidari in forum C Programming
    Replies: 3
    Last Post: 12-03-2010, 03:01 PM
  3. struct question...
    By Raigne in forum C++ Programming
    Replies: 10
    Last Post: 05-21-2007, 11:00 PM
  4. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  5. Question about list (struct {..struct *next}
    By jmzl666 in forum C Programming
    Replies: 1
    Last Post: 10-23-2002, 01:34 AM