0

I have char a[] = "abc".

I need to do: char b [] = a i.e essentially copy a to b without having to specify the size of b.

How can I do this in cpp?

14
  • char a[] in this case is a character array. If you need just one char then you can write char a = 'a'; Commented Jun 15, 2017 at 18:49
  • 7
    You're stuck if you stick with a character array, but std::string b = a; makes the whole nightmare go away. Commented Jun 15, 2017 at 18:49
  • 1
    @BhumiSinghal -- Arrays are dumb. They know nothing about their size. If you want this type of functionality, you have to go to a higher-level construct, such as std::string or some other container class that has = overloaded to do such copying. Commented Jun 15, 2017 at 18:56
  • 2
    @PaulMcKenzie That is incorrect. Arrays are fully aware of their size, it's encoded in their type, just like std::array. int[10] is an array of 10 ints. You can obtain the size of a C array using the ratio between the sizeof the array and the sizeof an element. You can also obtain it through template deduction. C arrays decay easily and frequently to pointers, but they are clearly distinct types. Having said that, please to not take this comment as encouragement to use of C arrays where safer and more powerful alternatives exist. Commented Jun 15, 2017 at 19:02
  • 1
    @PatrickRoberts -- just a bit of clarification on terminology. A C string is an array of char that's terminated by a nul character. Functions that take C strings treat that terminator as the end of the string. A string literal such as "abc" is an array of const char; it contains the characters between the quotes and a nul terminator, so it Can be used as a C string. A char array will have a nul terminator if you put one in it, and won't if you don't. If it has a nul terminator it can be used as a C string; if it doesn't, it can't. Commented Jun 15, 2017 at 20:23

1 Answer 1

2

There is no assignment operator overload for plain arrays. You have to use some copy function like this:

char a[] = "abc";
decltype(a) b;
std::copy( std::begin(a), std::end(a), std::begin(b) );

Using decltype we get the type of array a and use that to declare array b.

But this is still not very C++ish in my opinion.

As commenters suggested, simply use std::string. If you can't use std::string please explain why.

std::string a = "abc";
std::string b = a;

Much cleaner and less things that can go wrong.

Sign up to request clarification or add additional context in comments.

4 Comments

You don't really need the / sizeof(a[0]) since sizeof(char) is guaranteed to always be one. That said it does make it less brittle if the type ever changes.
@NathanOliver Yes the latter was the intend.
You could use decltype(a) b; instead of char b[sizeof(a)/sizeof(a[0])];
@rex That's pretty cool. Hadn't ever thought about doing that.

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.