[C] copy string question

A

Alan

hi, i want to separate a string into different parts and copy them to other
array, how can i do that?

Char s[11] = "12345.6789";
Char long_d[4] = "000",
long_m[8] = "00.0000";

e.g. i want to copy s to long_d and long_m so that
long_d[4] = "123" and long_m[8] = "45.6789"

thx
 
A

Artie Gold

Alan said:
hi, i want to separate a string into different parts and copy them to other
array, how can i do that?

Char s[11] = "12345.6789";
Erm, that's `char', not `Char'.
Char long_d[4] = "000", Similarly.
long_m[8] = "00.0000";

e.g. i want to copy s to long_d and long_m so that
long_d[4] = "123" and long_m[8] = "45.6789"

Use strncpy().

HTH,
--ag
 
A

Alan

Artie Gold said:
Alan said:
hi, i want to separate a string into different parts and copy them to other
array, how can i do that?

Char s[11] = "12345.6789";
Erm, that's `char', not `Char'.
Char long_d[4] = "000", Similarly.
long_m[8] = "00.0000";

e.g. i want to copy s to long_d and long_m so that
long_d[4] = "123" and long_m[8] = "45.6789"

Use strncpy().

but when i use
strncpy(long_d,s,3);
strncpy(long_m,s,7);
i just got long_d[4] = "123" and long_m[8] = "12345.6"
 
J

Jeff Schwab

Alan said:
Artie Gold said:
Alan said:
hi, i want to separate a string into different parts and copy them to
other
array, how can i do that?

Char s[11] = "12345.6789";

Erm, that's `char', not `Char'.
Char long_d[4] = "000",
Similarly.

long_m[8] = "00.0000";

e.g. i want to copy s to long_d and long_m so that
long_d[4] = "123" and long_m[8] = "45.6789"

Use strncpy().


but when i use
strncpy(long_d,s,3);
strncpy(long_m,s,7);
i just got long_d[4] = "123" and long_m[8] = "12345.6"

You can always ignore any number of characters at the beginning of the
string (up to the length of the string), and still have a valid string.
In this case, you want to skip the first three:

strncpy( long_m, s + 3, 7 );

Since the string you want is indicated by the address of the fourth
element (which has index 3), you could also do it this way:

strncpy( long_m, &s[3], 7 );
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top