initializing char array inside a struct

This is a discussion on initializing char array inside a struct within the C Programming forums, part of the General Programming Boards category; hi there! here's my question. i've been struggling about 2 hours with this but haven't figured it out yet. suppose ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    28

    initializing char array inside a struct

    hi there!

    here's my question. i've been struggling about 2 hours with this but haven't figured it out yet.

    suppose we have this string array

    Code:
    char index [INDEXSIZE][STRINGSIZE]={"debug","itterations","population",
    		"no fix vehicles","pm","pc"};
    indexsize is 6, stringsize is 50.

    modifying my program required that i will have to implement this array inside a struct. so i did this:

    Code:
    struct{
    		char index [INDEXSIZE][STRINGSIZE]={"debug","itterations","population",
    		                                            "no fix vehicles","pm","pc"};
    			
    		int val[INDEXSIZE];		
              }st1;
    but when i compile it i get the following message
    Code:
    teststring.c:20: error: expected :, ,, ;, } or __attribute__ before = token
    i have faced a somewhat similar problem in another code example and i had to initialize each array element one by one. this concept seems pretty silly to me so please tell me: WHAT AM I DOING WRONG ?

    i tested many different ways (variable length, character pointer, const specifier, different bracket concept etc) but nothing seems to work...

    thanks in advance

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    You can't initialize a variable inside a struct.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    28
    Quote Originally Posted by MacGyver View Post
    You can't initialize a variable inside a struct.
    agreed but how do i initialize it? i tried
    Code:
    char *index[INDEXSIZE];
    inside the struct body while trying to initialize outside with this:
    Code:
    st1.index[]={"debug","itterations","population",
    		"no fix vehicles","pm","pc"};
    but i get the following message
    Code:
    teststring.c:25: error: expected expression before ] token
    i tried several alternatives (changing the array size declaration, leaving it flexible etc) but i get the above message or something alternative with "{" . i know something is wrong with the expression i use but i can't figure out what...

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    You can't set a variable like that except when you initialize it upon declaration, which you can't do if it's a struct variable.

    If you don't need to ever alter the string, then just make it const and global and share it amongst the struct members.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You can do C99 style declarations at declare time,
    Code:
    struct{
        char index [INDEXSIZE][STRINGSIZE]={"debug","itterations","population",
    		                                            "no fix vehicles","pm","pc"};
        int val[INDEXSIZE];		
    }st1 = {{"debug","itterations","population", "no fix vehicles","pm","pc"}, {0, 0, 0, 0, 0, 0}};
    Or whatever it is, google it...

    However since it's C99, it's not entirely portable nor is it wise.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    No C99 required.
    Code:
    struct
    {
       char index [INDEXSIZE][STRINGSIZE];
       int val[INDEXSIZE];    
    } st1 = 
    {
       {
          "debug","itterations","population", "no fix vehicles","pm","pc"
       },
    };
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Jun 2006
    Posts
    28
    By the time i was in the middle of trying post #4, i "copy paste" #6 and worked just fine. my deep thanks to all of you guys, you're great.

    ps. i just found #6 initialization in "pointers on c" p226. perhaps i should open more frequently this treasure chest...

    thanks alot...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  2. Replies: 7
    Last Post: 06-16-2006, 10:23 PM
  3. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 10:57 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

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