help on initializing array

This is a discussion on help on initializing array within the C Programming forums, part of the General Programming Boards category; here is my code: /* ===== SharedDisk.h ===== */ #ifndef __SHAREDISK__ #define __SHAREDISK__ typedef struct { char Jump[ 3 ]; ...

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

    help on initializing array

    here is my code:

    /* ===== SharedDisk.h ===== */

    #ifndef __SHAREDISK__
    #define __SHAREDISK__


    typedef struct
    {
    char Jump[ 3 ];
    char Manufacturer[ 8 ];
    char BytesPerSector[ 2 ];
    char SectorsPerCluster;
    char ReservedSectors[ 2 ];
    char FATs;
    char RootDirectoryEntries[ 2 ];
    char LogicalSectors[ 2 ];
    char MediaDescriptorByte;
    char SectorsPerFAT[ 2 ];
    char SectorsPerTrack[ 2 ];
    char Surfaces[ 2 ];
    char HiddenSectors[ 4 ];
    } Disk;

    #endif


    initialize them somewhere in my program as following:
    theDisk->Jump = "";
    theDisk->Manufacturer = "";
    :
    :
    and so on.

    is this right?
    if not, how to initialize them?

    thx for the help

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    In a word, no. You can't assign char arrays values in that manner, you must use strcpy(). But, as you are setting all the arrays to be empty (I presume you are), it'd probably be easier to just memset() the complete struct to 0.

    >memset(theDisk, '\0', sizeof Disk);
    should do the trick.

    The strcpy() method is
    >strcpy(theDisk->Manufacturer, "ACME");
    Just make sure each element is big enough to hold the string plus one byte for the null terminator.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    151
    >In a word, no. You can't assign char arrays values in that manner, you must use strcpy(). <

    Can't you do -

    Disk theDisk = {0};

    ?

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Enmeduranki
    >In a word, no. You can't assign char arrays values in that manner, you must use strcpy(). <

    Can't you do -

    Disk theDisk = {0};

    ?
    Yes, you can Well, you could if theDisk was a structure, but if you look closely, you'll notice it's a pointer

    >Disk myDisk = {0};
    >Disk *theDisk = &myDisk;
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    9
    thx for the help
    that helps me a lot

  6. #6
    Banned borko_b's Avatar
    Join Date
    Jun 2002
    Location
    Well... I live in Bulgaria :)
    Posts
    100
    Code:
    typedef struct 
    { 
       char Jump[ 3 ]; 
       char Manufacturer[ 8 ]; 
       char BytesPerSector[ 2 ]; 
       char SectorsPerCluster; 
       char ReservedSectors[ 2 ]; 
       char FATs; 
       char RootDirectoryEntries[ 2 ]; 
       char LogicalSectors[ 2 ]; 
       char MediaDescriptorByte; 
       char SectorsPerFAT[ 2 ]; 
       char SectorsPerTrack[ 2 ]; 
       char Surfaces[ 2 ]; 
       char HiddenSectors[ 4 ]; 
    } Disk; 
    
    ....
    Disk theDisk;
    memset(&theDisk,0,sizeof(Disk));
    ....

  7. #7
    Banned borko_b's Avatar
    Join Date
    Jun 2002
    Location
    Well... I live in Bulgaria :)
    Posts
    100
    Or...
    Code:
    Disk *ptheDisk = &someDisk;
    
    memset(ptheDisk,0,sizeof(Disk));

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Initializing a 2D Array in C
    By Cell in forum C Programming
    Replies: 20
    Last Post: 03-21-2009, 12:31 PM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 12:01 AM
  5. Initializing char * array
    By Tia in forum C Programming
    Replies: 6
    Last Post: 03-11-2003, 04:19 PM

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