3

I am currently analysing the char and string datatypes. For char data types, the following code snipped holds good:

char value = 'a';
char value1[] = "Good";
char* value2 = "Good";

For strings,

string strValue = "Good";
string strVal[3] = {"Good","Better","Best"};

But the assignment below throws the compilation error:

"error: scalar object strPtr requires one element in initializer"

string* strPtr = {"Good","Better","Best"}

So, how to assign values to above string* initially?

Thanks, Udhai

5
  • 4
    char* value2 = "No good"; -- Through an error in language design, it is allowed, but it shouldn't be, and it is deprecated. String literals reside in read only memory, and in that statement, you are assigning a non-const pointer to the first element of one. So if you try to modify the string, it will compile, but it will not work at runtime. That's bad. Jul 27, 2012 at 6:22
  • Pointers point to an address; right? Therefore, when you "assign" a value to a pointer, you must first de-reference it, which then allows you to actually work with the data at the address the pointer points to. Trying to assign a value to a non-de-referenced pointer is saying that you're trying to change the address of the data you're pointing to. What you assign to a string * is an already defined, or at least declared, string. Jul 27, 2012 at 6:29
  • Hasn't that been fixed in the meantime? I thought nowadays (C++ 0x or maybe a later version of the standard) string literals always have the type const char* (and this should yield a compilation error).
    – Axel
    Jul 27, 2012 at 6:32
  • For the record, the GCC flag -Wwrite-strings helps debugging the issue pointed out by Benjamin. Jul 27, 2012 at 6:32
  • What's the use case for string* strPtr = {"Good","Better","Best"}? If your declare as string[], you can assign to string*, so there should normally be no need to do this. Since initialising this way places the array on the stack (thus you must not call delete[] on the pointer), and reassigning the pointer later to something usally allocated on the heap is just asking for trouble.
    – Axel
    Jul 27, 2012 at 6:40

3 Answers 3

3

Pointers are not arrays, so why would you want to pretend otherwise? You could do this however

string strVal[3] = {"Good","Better","Best"};
string* strPtr = strVal;
0

This works:

std::string* strPtr{new std::string[3]{"Good","Better","Best"}};

This also compiles

std::string* strPtr{(std::string[3]){"Good","Better","Best"}};

but binds the pointer to a temporary, so please don't do that.

2
  • In the first example, you create the array on the heap, not stack, and have to delete manually if you don't want your memory to leak.
    – Axel
    Jul 27, 2012 at 6:29
  • @Axel: That depends on the lifetime... If it's a global, there's no need to ever deallocate it, since globals should live until the end of the program.
    – Ben Voigt
    Jul 27, 2012 at 13:45
-1

I suppose you refer to std::string.

The correct way is

std::string *s(new std::string);
// or
std::string *s = new std::string;

if you want a pointer to a string and

std::string s_arr[3] = {"Good","Better","Best"};
string* str_p = s_arr;

if you want a pointer pointing to the start of an array of strings.

1
  • ...(new std::string; are you missing a )? Jul 27, 2012 at 6:16

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.