Thread: strcopy for an array

  1. #1
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455

    strcopy for an array

    suppose i have an array of size 12, and i wanted to strcpy() it into a c-style string. any "quick" way of doing this? its complaining about casting and wrong types..its starting to annoy me. then i got past some of the problems...and then it gaved casting problems, like to a constant, i tried const_cast<> but no luck..

    ex:

    char array[12];
    char *newstring;
    strcpy(newstring, &array);

    something like that...

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >char *newstring;
    >strcpy(newstring, &array);
    And where were you planning on putting these 12 characters in a freshly declared local pointer? Provided you allocate memory to newstring and provided array actually ends with a nul character
    Code:
    strcpy ( newstring, array );
    should work okay for you.
    Last edited by Prelude; 11-03-2003 at 09:06 PM.
    My best code is written with the delete key.

  3. #3
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    it keeps saying

    c:\Documents and Settings\Nabeel.Shahzad\My Documents\Visual Studio Projects\temp_mon\daemon\cmbm.cpp(103): error C2664: 'strcpy' : cannot convert parameter 2 from 'unsigned char [12]' to 'const char *'

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Post the actual code you're using. I suspect you're not using the correct syntax. A static array variable without square braces should resolve to an LPCHAR, and the error you received implies that you wrote:
    Code:
    strcpy(newstring,array[]);
    //or
    strcpy(newstring,array[12]);
    It should be:
    Code:
    lstrcpy(newstring,array);
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > cannot convert parameter 2 from 'unsigned char [12]' to 'const char *'
    You're still doing &array (as per your original example)
    Lose the &
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    just wondering... for the example he gave, would this work:
    Code:
    ...
    char array[12];
    char*newstring=new char[strlen(array)];
    int i;
    
    for(i=;i<strlen(newstring);i++)
         newstring[i]=array[i];
    
    newstring[i]='\0';
    ...
    I'm not asking if it's efficient, just if it would work...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    51
    first thing, i guess u must initialise array, second thing, array length != 0

    third in the loop u can't use strlen(newstring), it must be strlen(array).

    newstring hasn't been terminated by the program so the length of newstring could be anything.

  8. #8
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    thanks... I don't feel much like thinking now, so can you please remind me if that last line is right, or should it be:
    Code:
    newstring[i+1]='\0';
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  9. #9
    Registered User
    Join Date
    Aug 2003
    Posts
    51
    no the way u've done it, it should be newstring[i] = '\0';

    but personally this is the way i would do it.

    since '\0' character value is 0, the loop will end with that value.

    Code:
    for(i = 0; array[i]; ++i)
        newstring[i] = array[i]
    
    newstring[i] = '\0';
    actually i missed something in ur code above, it shouldn't be

    Code:
    char *newstring = new char[strlen(array)];
    it should be

    Code:
    char *newstring = new char[strlen(array)+1];
    requires the +1 for the '\0' terminator
    Last edited by Kyro; 11-04-2003 at 08:16 AM.

  10. #10
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    let my try majors idea


    here is my code
    Code:
    struct ... {
    unsigned char ssName[12];             // name of sensor
    }
    
    .....
    strcpy(name, data->sdSensor[n_sensor].ssName);

    edit:
    Code:
    char *retval = new char[13];
    		for(int i=0;i<13;i++){
    			retval[i]=data->sdSensor[n_sensor].ssName[i];
    		}
    		retval[12]='\n';
    this worked..but i dont like the loop thing. and strlen() didnt work on the array, but luckily i know the array's size anyway



    oh, and i dont know if the string arrays are null terminated or not, i assume they are...the values are being read through OpenFileMapping
    Last edited by the Wookie; 11-04-2003 at 09:02 AM.

  11. #11
    Registered User
    Join Date
    Aug 2003
    Posts
    51
    first one, u can't convert unsigned char[] to char*

    in other words u can't have

    Code:
    char *pc = 'l';
    unsigned char *upc = pc;
    u'll get a compiler error without a type cast.

    the second one looks fine. what was wrong with strlen? if u pass data->sdSensor[n_sensor].ssName to it, it should work.

  12. #12
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    yeah, it should..but it doesnt. im trying something else

  13. #13
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Originally posted by Kyro
    it should be

    Code:
    char *newstring = new char[strlen(array)+1];
    requires the +1 for the '\0' terminator
    yeah, i just forgot the +1 in there... thanks though...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  14. #14
    Registered User
    Join Date
    Aug 2003
    Posts
    51
    oh gee, i'm half asleep, it doesnt work on the array because data->sdSensor[n_sensor].ssName is type unsigned char[]. U can't pass an unsigned char[] or unsigned char* to strlen. sorry about that.

    i think i'll go to sleep now :S

  15. #15
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    is there an easy way to change the type of a string besides looping through each element and casting it to another string of a different data type?
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM