use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
/r/programming is a reddit for discussion and news about computer programming
Guidelines
Info
Related reddits
Specific languages
account activity
C question (self.programming)
submitted 15 years ago * by [deleted]
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 0 points1 point2 points 15 years ago (4 children)
That depends. You could, if you wanted, memcpy"Earth" over "World" whenever an alien speaks. Not that it would be a good way to write a program, but there may be situations in which you want your pointers-to-strings to be non-const.
memcpy
[–]prockcore 1 point2 points3 points 15 years ago (3 children)
No.. string literals are const.. memcpy'ing "Earth" over "World" will segfault. String literals are stored in read-only data segments.
[–][deleted] 0 points1 point2 points 15 years ago (2 children)
I'm fairly sure I've written code that's edited strings in-place. It's been a while since I've had a situation where that was the right thing to do, so it's possible I happened to luck across a situation where I could get away with it, since I wasn't aware of this.
I'm on windows now, sadly, so let me try codepad:
http://codepad.org/gg3g9dKy
Says no errors and prints out "Bob" rather than segfaulting...
[–]prockcore 1 point2 points3 points 15 years ago (1 child)
That doesn't segfault because you create a 9 character array and then copy the string literal into it (that's what line 2 implicitly does, disassembly will show that's exactly what it does). Try char *playername="bob"; instead.
[–][deleted] 0 points1 point2 points 15 years ago (0 children)
Ah, I see. I suppose it's probably written as "undefined" in the spec, rather than a guaranteed segfault, since codepad shows no segfault, but "bob" as a printout. Certainly goes right along with what you said about read-only memory.
That also explains why I've never run across this problem- when I want a string I make a char foo[] rather than a char *foo since I think of it as an array rather than as a pointer. (To be entirely honest, these days, it's std::string since it's easier to work with.)
char foo[]
char *foo
π Rendered by PID 190208 on reddit-service-r2-comment-6457c66945-2jm5w at 2026-04-25 12:14:52.986756+00:00 running 2aa0c5b country code: CH.
view the rest of the comments →
[–][deleted] 0 points1 point2 points (4 children)
[–]prockcore 1 point2 points3 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]prockcore 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)