setting structure char array with string

This is a discussion on setting structure char array with string within the C Programming forums, part of the General Programming Boards category; Hello, sorry if this seems like a trivial query but I was having some issue setting a string to a ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    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.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    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
    India
    Posts
    515
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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

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