| | |
char * = "string" and strcpy = SegFault?
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Aug 2006
Posts: 13
Reputation:
Solved Threads: 0
I get a segfault when I run the following. Seems to fail at strcpy.
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.
C++ Syntax (Toggle Plain Text)
#include <cstring> using std::strcpy; int main() { char * word = "word"; strcpy(word, "what"); }
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.
•
•
•
•
I get a segfault when I run the following. Seems to fail at strcpy.
C++ Syntax (Toggle Plain Text)
#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.
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? •
•
Join Date: Aug 2006
Posts: 13
Reputation:
Solved Threads: 0
•
•
•
•
They both do nearly the same thing. The version with achar * 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 line5=7;and you'll get an error.
wheras the version withchar 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.
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.
C++ Syntax (Toggle Plain Text)
const char * word = "word"
Last edited by saishn; Aug 27th, 2006 at 1:29 pm.
•
•
•
•
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.
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.
![]() |
Similar Threads
- How to extract words from char type string (C++)
- Deleting Characters Function (C)
- my #include <string> wont make "string" bold (C++)
- google "keyword" question (Search Engine Optimization)
Other Threads in the C++ Forum
- Previous Thread: Errors While Compiling
- Next Thread: Want a free compiler for C/C++ under Windows
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





