0

Our instructor told us that a string is the array of characters, and I was wondering whenever we use any array statically, we have to define its size before compiling the pro-gramme in C++ then why don't we do same with the string? Thanks in advance.

6
  • 1
    Syntactic sugar (the compiler knows the length because it can count the characters in the string literal). As an aside, A C++ instructor probably should be saying that strings are instances of the std::string class, not arrays of char. Commented Mar 3, 2015 at 16:29
  • 1
    std::string is a class that encapsulates a "string". A C "string" in C++ is an array of characters, and must be declared with a size (e.g. char buffer[256]) or used as a string literal (e.g. "Hello, World!") (not always interchangeable). Commented Mar 3, 2015 at 16:30
  • How does compiler know the length of string when we take input from the user in the string?? User can give strings with different lengths Commented Mar 3, 2015 at 16:34
  • You can, however, still access each character of your string like an array cout << someString[3]. As for length, it would be someString.length() or, for a c string, strlen(someString) Commented Mar 3, 2015 at 16:35
  • 1
    @Nilesh, if the string is not a literal, then the compiler cannot help you, and you have to allocate enough room to accommodate the string entered by the user (or truncate that string). Buffer overflows are what happens when you fail to do that. Commented Mar 3, 2015 at 16:36

3 Answers 3

2

The compiler can choose an array's size automatically to match its initial content, for example:

int a[] = { 3, 5, 2 };

So this is not something that string literals have and other arrays don't.

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

Comments

1

The string is an object, which is smarter than a character array. A character array is just an allocation in memory, it has no logic associated with it. However the string (because it is an object) is able to manage its own memory and expand as needed. In C++ you can overload operators. Because the string class has its [ ] operators overloaded you can use the string as an array and access individual characters. However when you use the [ ] operators you are actually invoking a method on the string (namely operator[ ]).

So you can create a string, expand by adding to it, and access individual characters in it:

string str1 = "Hello "; // create a string and assign value
string str2("World");   // use the constructor to assign a value
str1 += str2;           // append one string to another

cout << str1[0];        // should print H

But even though the opeartor overloading give it has the same feel as an array, it's actually an object.

Comments

0

if we talk about char* arr = "hello world"; now here "hello world" is given memory through a string object and the object is initialized by the constructor of the String class.

if we say String str = "hello world"; here again constructor of String class is called and it initializes the str object of String to point to the starting address of "hello world" which is stored somewhere in memory.

here we do not have to give the size, instead of that constructor of string class is doing all the trick of allocating dynamic memory and initializing.

Comments

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.