I get a segfault when I run the following. Seems to fail at strcpy.

#include <cstring>
using std::strcpy;

int main()
{
    char * word = "word";
    strcpy(word, "what");
}

If I change char * word to char word[], it's fine. I thought that both declarations effectively do the same thing. What am I missing?

Thanks in advance.

EDIT: using c++ btw, but practicing using c style strings for the moment.

Recommended Answers

All 6 Replies

I get a segfault when I run the following. Seems to fail at strcpy.

#include <cstring>
using std::strcpy;

int main()
{
    char * word = "word";
    strcpy(word, "what");
}

If I change char * word to char word[], it's fine. I thought that both declarations effectively do the same thing. What am I missing?

Thanks in advance.

They both do nearly the same thing. The version with a char * word = "word"; creates a pointer to the string literal "word". You cannot assign to or overwrite a literal.
eg, try compiling a program containing the line 5=7; and you'll get an error.

wheras the version with char word[] = "word"; creates an array of 5 characters in memory; 'w', 'o', 'r', 'd', '\0' . since the array is an object rather than a literal, its contents may be changed.

commented: Good -[Grunt] +1

They both do nearly the same thing. The version with a char * word = "word"; creates a pointer to the string literal "word". You cannot assign anything to a literal, since a literal is constant.
eg, try compiling a program containing the line 5=7; and you'll get an error.

wheras the version with char word[] = "word"; creates an array of 5 characters in memory; 'w', 'o', 'r', 'd', '\0' . since the array is an object rather than a literal, its contents may be changed.

Thanks for the response Bench.

So char word[] = "word" copies the characters to a different memory location. While, the char * word points to the location where the literal "word" is stored and it's unmodifiable?

They should issue a compile error saying that I have to make a pointer to const data to make the assignment then.

const char * word = "word"

EDIT: I'm using gcc(g++) 3.2 btw.

Thanks for the link Grunt.

I missed it at first because it looked like part of the heading for your post. I thought the VC6 comment was your reply.

While, the char * word points to the location where the literal "word" is stored and it's unmodifiable?

Yes, it's unmodifiable. If you try to modify it then behaviour is undefined.

They should issue a compile error saying that I have to make a pointer to const data to make the assignment then.

@Stroustrup(5.2.2)
A string literal can be assigned to a char *. This is allowed because in previous definations of C and C++, the type of a string literal was char *. Allowing the assignment of a string literal to a char * ensures that millions of lines of C and C++ remain valid.

In addition to that, Bjarne Stroustrup clearly say that
(B2.3)
"Implicit conversion of a string literal to a (non-const) char* is deprecated.You should use array of char or avoid assignment of string literals."

So, there is a possibilty that standard committee might remove it in future versions but they are not under any compulsions to do that for sure.

commented: nice +1

I understand now. Thanks for all the info Bench and Grunt.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.