3

I was searching through SO and I came across this question of assigning fixed length character array to a string. But what I want is the reverse operation, assigning a string to a fixed length character array. For eg if I have

char name[20];
name = "John";

I get a compile time error saying I am assigning char array[4] to char array[20].

How can I get this done ?

5
  • Honestly, I would go for using string::c_str() instead. Commented Sep 5, 2012 at 4:31
  • @Chris How will that assign to an array :-) you know better than that Commented Sep 5, 2012 at 4:37
  • @AdrianCornish, Never mind, I was thinking of a dynamic array, but it has to be fixed-sized. I don't really see the point... Commented Sep 5, 2012 at 4:40
  • @chris - Agree that would work - but for an OP who does not know - you need to say how Commented Sep 5, 2012 at 4:40
  • This is not the reverse of the other question, because "John" is not a std::string. It is a string literal, and the data type is char [5], not std::string. C++ does not give any special treatment to std::string (and most other library classes and functions). Commented Sep 5, 2012 at 5:49

4 Answers 4

3

Use strncpy

strncpy(name, "John", sizeof(name)-1);

EDIT

As many others pointed out (I was wrong) - strncpy will not always null terminate the string so if you need to use it as a c string then it needs to be explicitly null terminated. It will not overflow it but that may not be all you need

if(sizeof(name)>0)
{
    name[sizeof(name)-1]=0;
}
Sign up to request clarification or add additional context in comments.

8 Comments

WARNING: strncpy does not append a null \0 to the end of the string; this is a (often fatal) problem if your input string is larger than the target string. Always manually write a null to the last entry in your character array!
If the source string is sizeof(name)-1 in length or greater the destination still won't be null terminated. All copying 1 less byte does is leave the last byte of the array unchanged from whatever it was. If you want to make sure that last byte is 0 you need to explicitly set it.
"destination will only be null-terminated if the length of the C string in source is less than num." If null-termination is needed, then, after the strncpy, add this : name[sizeof(name)-1] = '\0';
The destination array is 10 characters in length. You tell strncpy the array is 9 characters in length and ask it to copy a 9 character string. How does it know the destination is really 10 characters in length? The last byte of the array, which is outside what you told strncpy is ok to touch, is unchanged.
@AdrianCornish strncpy null terminates in this case because strlen("John") < sizeof(name)-1 not because you added -1. strncpy no more knows the real size of the array you are copying to than any other C function, so just saying sizeof(array)-1 does not help. To be safe you need the explicit null termination that Lee used.
|
1
char    dst[30];
string  src("hello");
// check here that sizeof(dst) > src.size()
copy(begin(src), end(src),  begin(dst));
dst[src.size()] ='\0';

std::string also has c_str() method. It is expansive - dynamically allocates memory. You can use returned const char* if don't mind const . This c-string will be destructed when src is destructed.

Comments

0

Passing a Constant String wont do the Job as it wont append \0 (escape Characters) to the rest of your array !! strcpy does this automatically for you , all the free spaces are filled with escape characters , thus you get no error !!

Comments

-1

Try using strcpy

http://www.cplusplus.com/reference/clibrary/cstring/strcpy/

basically you will want to strcpy(name,"John");

6 Comments

strcpy is dangerous to use due to overflow of the target array
True. You could always just declare the variable with the string value. Ie char name[]="John"; Even using strncopy you can still loose precision if the string is longer than the array.
@daveh Consider suggesting strncpy.
@AlexBrown Stack attack issues?
Im assuming he means buffer overflow
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.