Problem with array of structs

This is a discussion on Problem with array of structs within the C Programming forums, part of the General Programming Boards category; I have decalred a struct using: Code: struct MEM { char MEMarray[7]; }; And declared an array of structs using ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    145

    Problem with array of structs

    I have decalred a struct using:

    Code:
    struct MEM {
           char MEMarray[7];
           };
    And declared an array of structs using

    Code:
    struct MEM arrayOfStructs[10];
    How do I then start storing stuff into my MEMarray?

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,075
    Code:
    strcpy(arrayOfStructs[0].MEMarray,"Hello!");
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    Ah very clever, thanks.

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    here is one more example for you

    Code:
    #include<stdio.h>
    
    struct SomeTestStruct
    {
        int SomeDataAsInt;
        char SomeDataAsString[20];
        float SomeDataAsFloat;
    };
    
    int main()
    {
        struct SomeTestStruct SomeTestStructArray[5] = {{10,"Test 1",20.5},
                                                        {20,"Test 2",30.5},
                                                        {30,"Test 3",40.5},
                                                        {40,"Test 4",50.5},
                                                        {50,"Test 5",60.5}};
       int  i;
       
       printf("This is Struct Initialization\n");
       printf("=============================\n\n");
       for(i=0;i<5;i++)
          printf("%d %s %f\n",SomeTestStructArray[i].SomeDataAsInt,SomeTestStructArray[i].SomeDataAsString,
                            SomeTestStructArray[i].SomeDataAsFloat);
       
      getchar();
      return 0;
    }   
    
    /*my output
    This is Struct Initialization
    =============================
    
    10 Test 1 20.500000
    20 Test 2 30.500000
    30 Test 3 40.500000
    40 Test 4 50.500000
    50 Test 5 60.500000
    
    */
    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with copying a string into array in a struct
    By JFonseka in forum C Programming
    Replies: 15
    Last Post: 05-04-2008, 06:07 AM
  2. Problem with file and array
    By paok in forum C Programming
    Replies: 5
    Last Post: 05-01-2008, 05:19 AM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  4. Array of Structs question
    By WaterNut in forum C++ Programming
    Replies: 10
    Last Post: 07-02-2004, 03:58 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM

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