Thread: copying of char

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    26

    copying of char

    Code:
     
    for( j=0; j< 3; j++)
                          { TmpPathRec.leg2_carry_cost[j] = fss_leg_found->carry_cost[j]; }
    in header file
    short   carry_cost[3];
    short leg1_carry_cost[3];
    Previously we are using carry_cost[j] this for numeric.But now i want use it for char
    I want to conform whether below code is right or not?
    Code:
    for( j=0; j< 3; j++)
    { strncpy(TmpPathRec.leg1_c64[j],fss_leg_found->c64[j],C64_LEN); }
    in header file
    char   c64[3][C64_LEN];
    char leg1_c64[3][C64_LEN];
    Waiting for valuable replies

    Thanks & Regards,
    vijay,

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, that seems like a possible solution. Note however that if the string is really C64_LEN long, then you will not have a terminating zero in the copy of the string.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    26

    thanks matstap

    but i defined it as #define C64_LEN 3

    Thanks & regards,
    vijay,

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by vijay85 View Post
    but i defined it as #define C64_LEN 3

    Thanks & regards,
    vijay,
    And? It doesn't matter if it is 3 or 400. If you have C64_LEN characters in the string, then it will not have a terminating zero, so it is not a valid C string. That's not to say that's the wrong thing to do - just that you need to be aware of that possibility.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  2. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 02:06 PM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  4. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 12:25 PM
  5. I'm having a problem with data files.
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 05-14-2003, 09:40 PM