0

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.

3
Contributors
6
Replies
9
Views
10 Years
Discussion Span
Last Post by saishn
Featured Replies
  • 2

    [QUOTE=saishn;246937]I get a segfault when I run the following. Seems to fail at strcpy. [code] #include <cstring> using std::strcpy; int main() { char * word = "word"; strcpy(word, "what"); } [/code] If I change char * word to char word[], it's fine. I thought that both declarations effectively do the … Read More

  • [quote=saishn;246944]While, the char * word points to the location where the literal "word" is stored and it's unmodifiable? [/quote] Yes, it's unmodifiable. If you try to modify it then behaviour is undefined. [quote=saishn;246944]They should issue a compile error saying that I have to make a pointer to const data to … Read More

2

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.

Votes + Comments
Good -[Grunt]
0

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.

0

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.

1

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.

Votes + Comments
nice
0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.