char * = "string" and strcpy = SegFault?

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Aug 2006
Posts: 13
Reputation: saishn is an unknown quantity at this point 
Solved Threads: 0
saishn saishn is offline Offline
Newbie Poster

char * = "string" and strcpy = SegFault?

 
0
  #1
Aug 27th, 2006
I get a segfault when I run the following. Seems to fail at strcpy.
  1. #include <cstring>
  2. using std::strcpy;
  3.  
  4. int main()
  5. {
  6. char * word = "word";
  7. strcpy(word, "what");
  8. }
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.
Last edited by saishn; Aug 27th, 2006 at 1:13 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 492
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 49
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: char * = "string" and strcpy = SegFault?

 
2
  #2
Aug 27th, 2006
Originally Posted by saishn View Post
I get a segfault when I run the following. Seems to fail at strcpy.
  1. #include <cstring>
  2. using std::strcpy;
  3.  
  4. int main()
  5. {
  6. char * word = "word";
  7. strcpy(word, "what");
  8. }

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.
Last edited by Bench; Aug 27th, 2006 at 1:33 pm.
¿umop apisdn upside down?
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 147
Reputation: Grunt has a spectacular aura about Grunt has a spectacular aura about 
Solved Threads: 12
Grunt's Avatar
Grunt Grunt is offline Offline
Junior Poster

Re: char * = "string" and strcpy = SegFault?

 
0
  #3
Aug 27th, 2006
The key to eliminating bugs from your code is learning from your mistakes.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 13
Reputation: saishn is an unknown quantity at this point 
Solved Threads: 0
saishn saishn is offline Offline
Newbie Poster

Re: char * = "string" and strcpy = SegFault?

 
0
  #4
Aug 27th, 2006
Originally Posted by Bench View Post
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.
  1. const char * word = "word"
EDIT: I'm using gcc(g++) 3.2 btw.
Last edited by saishn; Aug 27th, 2006 at 1:29 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 13
Reputation: saishn is an unknown quantity at this point 
Solved Threads: 0
saishn saishn is offline Offline
Newbie Poster

Re: char * = "string" and strcpy = SegFault?

 
0
  #5
Aug 27th, 2006
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.
Last edited by saishn; Aug 27th, 2006 at 1:34 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 147
Reputation: Grunt has a spectacular aura about Grunt has a spectacular aura about 
Solved Threads: 12
Grunt's Avatar
Grunt Grunt is offline Offline
Junior Poster

Re: char * = "string" and strcpy = SegFault?

 
1
  #6
Aug 27th, 2006
Originally Posted by saishn View Post
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.

Originally Posted by saishn View Post
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.
The key to eliminating bugs from your code is learning from your mistakes.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 13
Reputation: saishn is an unknown quantity at this point 
Solved Threads: 0
saishn saishn is offline Offline
Newbie Poster

Re: char * = "string" and strcpy = SegFault?

 
0
  #7
Aug 27th, 2006
I understand now. Thanks for all the info Bench and Grunt.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC