Assigning Values in Structs

This is a discussion on Assigning Values in Structs within the C Programming forums, part of the General Programming Boards category; OK, I've managed to iron out all the bugs in my code, except for in the following code: Code: typedef ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    19

    Post Assigning Values in Structs

    OK, I've managed to iron out all the bugs in my code, except for in the following code:

    Code:
    typedef struct
    {
    	char surname[20];
    	char name[15];
    	int addLevel = 1;
    	int subLevel = 1;
    	int correct = 0;
    	int wrong = 0;
    	int correctSession = 0;
    	int wrongSession = 0;
    } USER;
    Which returns the following errors:

    Error "c:\lee's folder\school files\cse1301\iproject\main.c": 10 Syntax error; missing semicolon before `='
    Error "c:\lee's folder\school files\cse1301\iproject\main.c": 10 skipping `=' `1'
    Error "c:\lee's folder\school files\cse1301\iproject\main.c": 11 Syntax error; missing semicolon before `='
    Error "c:\lee's folder\school files\cse1301\iproject\main.c": 11 skipping `=' `1'
    Error "c:\lee's folder\school files\cse1301\iproject\main.c": 12 Syntax error; missing semicolon before `='
    Error "c:\lee's folder\school files\cse1301\iproject\main.c": 12 skipping `=' `0'
    Error "c:\lee's folder\school files\cse1301\iproject\main.c": 13 Syntax error; missing semicolon before `='
    Error "c:\lee's folder\school files\cse1301\iproject\main.c": 13 skipping `=' `0'
    Error "c:\lee's folder\school files\cse1301\iproject\main.c": 14 Syntax error; missing semicolon before `='
    Error "c:\lee's folder\school files\cse1301\iproject\main.c": 14 skipping `=' `0'
    Error "c:\lee's folder\school files\cse1301\iproject\main.c": 15 Syntax error; missing semicolon before `='
    Error "c:\lee's folder\school files\cse1301\iproject\main.c": 15 skipping `=' `0'

    So, I'm guessing that this means I can't assign values to struct members during declaration. Is there an easy way around this, or am I going to have to do this the hard way?

    Thanks in advance,
    Kirby1024

  2. #2
    Registered User Zeeshan's Avatar
    Join Date
    Oct 2001
    Location
    Munich, Germany
    Posts
    224

    Smile

    If it was C++ that you were using you could've used a contructor function in the structure for assignment of initial values.

    I think there's no such alternative in C, so "you'll have to do it the hard way"

    i.e. once you declare a variable of type USER you'll have to initialize each and every member variable separately.

    or as under;

    USER no1 = {"name","sirname",1,1,0,0,0};

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    19
    OK, well thanks anyway.

    Ah well. At least I won't make the same mistake again...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structs, pointers and functions
    By osici in forum C Programming
    Replies: 2
    Last Post: 04-29-2009, 12:35 AM
  2. Replies: 2
    Last Post: 11-26-2008, 09:25 AM
  3. Sending values to a control
    By Zyk0tiK in forum C Programming
    Replies: 6
    Last Post: 12-02-2005, 05:29 PM
  4. accessing array of structs values
    By WaterNut in forum C++ Programming
    Replies: 12
    Last Post: 07-08-2004, 08:47 PM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 12:38 PM

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