1

I am used to programming in Java and am doing something now which involves a little C++.

I am not actually learning C++, I am actually using assembly code, it is the C++ part that I have no idea about.

I have looked at past code from my lecturer (University), and to define a variable containing what I (being a Java developer) would call a String, he declared it as a char[] (I assume this means char/character array).

char get1[] = "Give me the first number:";

Does C++ use char arrays to mean Strings or are they different?

For the extremely limited C++ I am using, should I assume them to be the same and use char arrays as my lecturer does?

All I will be doing with this string is printing it to the screen as certain point (Using printf if any of you know intel x86 assembly). Should I be using char arrays?

Thank you.

PotWashMike
  • 199
  • 1
  • 2
  • 8
  • 1
    http://stackoverflow.com/questions/1287306/difference-between-string-and-char-types-in-c – ford prefect Oct 31 '13 at 16:49
  • 2
    What do you mean by *string*? `std::string` or `char const *str = "I am a string literal";`? – Praetorian Oct 31 '13 at 16:50
  • A `[const] char` array terminating with `\0` is referred to as a *C-string*. There are conversion constructors in `std::string` that take a C-string but they are not the same thing. Also, a notable difference is that the length of C-string is always one higher than the `std::string` constructed from that C-string (because of the termination character - i.e. `strlen(str) > std::string(str).size()`). – thokra Oct 31 '13 at 16:50
  • see also: http://stackoverflow.com/questions/1466073/how-is-stdstring-implemented – Inisheer Oct 31 '13 at 16:53
  • @Praetorian, a sequence of characters. I don't have a clue what std:string means but the second thing "I am a string literal" is what I am referring to. – PotWashMike Oct 31 '13 at 16:59
  • All I will be doing with this string is printing it to the screen as certain point (Using printf if any of you know intel x86 assembly). Should I be using char arrays? – PotWashMike Oct 31 '13 at 17:00

2 Answers2

2

A std::string is a class with functionality. A char array is just that - an array of characters.

You can colloquially call a null-terminated char array (such as your example) a C-style string, but that's just a name.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
1

Char arrays are certainly a kind of string - c++ programmers and API documentation often refer to char arrays as "c strings", or "c-style strings" (to differentiate them from CStrings, which are a Microsoft concept and something totally different).

One of the most annoying things about c++ as a language is that it doesn't define a single string concept as native to the language, exactly, so there are a number of constructs that all implement strings. std::string (or, if you use unicode, which you should, std::wstring) is the most common implementation, being cross-platform and basically part of the language at this point, but if you use Microsoft code you'll also see APIs that call for strings of type BSTR, _bstr_t, CComBSTR, and CString - all of these are also string types!

In general, I'd use std::[w]string if given the choice, and convert strings to (w)char-arrays as necessary - though if your string operations are pretty simple, and your output is going to have to be a c-string anyway, it wouldn't be that weird to do everything in terms of c-strings.

neminem
  • 2,658
  • 5
  • 27
  • 36
  • `std::wstring` is not unicode string. It is a string of `wchar_t` type, and `wchar_t` is only UCS-2 compliant on Windows. On Linux, a Unicode string would be `std::string`, which is usually encoded in UTF-8. – Siyuan Ren Oct 31 '13 at 17:18