Thread: setting structure char array with string

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    37

    setting structure char array with string

    Hello,

    sorry if this seems like a trivial query but I was having some issue setting a string to a char array in a structure. I have a set up like this:

    Code:
    typedef struct
    {
    int date; char name[20];
    } call[2];
    
    call iec;
    call.date = 1022009;
    call.name ="TEST";
    I can get the date set but not the string.


    Also the call[2] at the end of the structure definition this means that I can hold 2 structures right? Here I only set up one, called iec.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    #include <string.h>
    
    typedef struct
    {
       int date; 
       char name[20];
    } call; /* this is type */
    
    int main(void)
    {
       call iec; /* this is object of the type call */
       call something[2]; /* this is array of two objects of type call */
       
       iec.date = 1022009;
       strcpy(iec.name,"TEST"); /* this is a way to copy strings in C */
       return 0;
    }
    Last edited by vart; 04-03-2009 at 07:09 AM.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    Your whole declaration thing is wrong.
    It should be
    Code:
    typedef struct
    {
     int date;char name[20];
    } call;
    
    call iec;
    iec.date = 1022009
    strcpy(iec,name, "TEST");
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  4. #4
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    Quote Originally Posted by vart View Post
    Code:
    #include <string.h>
    
    typedef struct
    {
       int date; 
       char name[20];
    } call; /* this is type */
    
    int main(void)
    {
       call iec; /* this is object of the type call */
       call something[2]; /* this is array of two objects of type call */
       
       call.date = 1022009;
       strcpy(call.name,"TEST"); /* this is a way to copy strings in C */
       return 0;
    }
    Shouldn't that be
    Code:
    something[0].date = 1022009;
    ?
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    37
    Thanks you vart, much appreciated. It worked with a single object, will try with an array of structures now.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    37
    Also got it working for arrays. Great.

    Thanks again!

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by PING View Post
    Shouldn't that be
    Code:
    something[0].date = 1022009;
    ?
    yeah, have missed that part in the original code... fixed it
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. How to delete string after >
    By winsonlee in forum C Programming
    Replies: 5
    Last Post: 08-08-2004, 04:23 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM