-
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...
-
>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.
-
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 *'
-
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);
-
> cannot convert parameter 2 from 'unsigned char [12]' to 'const char *'
You're still doing &array (as per your original example)
Lose the &
-
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...
-
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.
-
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';
-
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
-
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
-
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.
-
yeah, it should..but it doesnt. im trying something else
-
Quote:
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...
-
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
-
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?