0

I am really used to java programing now I want to use cpp and I was wondering what is a string called in cpp pretty silly question ? i am trying to use int but the compiler seems not to understand

Zeyad Serag
  • 21
  • 1
  • 9

2 Answers2

6

In C++ a String is called string or preferably std::string and an int is called int. You wouldn't use an int instead of a string.

You appears to have two questions, one about string and another about int, which is confusing, but most likely you have a compilation error in your code which appears to be complaining about int when this is not the problem. I suggest you post a simple example of your code so we can see what you are trying to do.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

The following types can be used as "strings" in C++:

1) std::string (defined in <string>)

#include <string>

std::string s = "hello world";

2) array of char

char s[16] = "hello";
char s[] = "world";

3) pointer to char (may actually point to an array)

const char* const globalConstString = "hello world";

void functionThatChangesString(char* s)
{
    s[0] = '!';
}

Note that C-style char arrays and char pointers are less "safe" than C++ strings and should be used with care.

Kirinyale
  • 338
  • 2
  • 10
  • please take the pointer to char initialization out, in current c++ it's deprecated, literals have an array type. – oblitum Jun 08 '13 at 15:12
  • @chico, I am initializing a const pointer, and I believe that array-to-pointer conversion is perfectly legal as long as it is const-correct. I believe you are talking about conversion to char*, not to const char*? – Kirinyale Jun 08 '13 at 16:37
  • no, even const correct, at initialization, it's deprecated. Decaying to pointer in arguments is no problem, but initialize a pointer with an string literal is. – oblitum Jun 08 '13 at 16:42
  • sorry, you're correct, when you get const correctness, there's no problem. – oblitum Jun 08 '13 at 16:46