It's "hello" that is "constant" in the sense that modifying it is has undefined behavior, even if it might work on your implementation. Oh yea i read the faq, thanks a lot for pointing me there..
Thanks for your helpful answers, im learning. Chris Torek. When it is not an array initializer, it always produces an anonymous array. This array has "static duration" -- meaning it is valid during the entire execution of the program -- and may or may not reside in physically-read-only storage.
This optimization turns out to be relatively easy to make -- one just needs to collect all strings, group them by size, and then "compare backwards" to see if any one string is an exact match for the end of any equal-length-or-longer string.
Note that counted-length strings, which are being discussed in a separate ongoing thread in comp. There are two main ways to do counted-length strings, one of which prohibits sharing entirely and the other of which affords even more opportunities for sharing, so that a simple tail-match algorithm is insufficient. It remains in memory, because a static-duration array exists until the program exits and maybe even after that; the C standard necessarily says nothing about what happens before and after a program runs.
As for whether this memory is "wasted", ask yourself this question: if the memory containing the string were to be used for something else, how would it a get set up initially, and b get "restored" if the function in question were re-entered? Barry Schwarz. Not at all. Both arrays exist in your program for the life of the program.
Therefore, they cannot occupy the same memory. At the start, myString contains the address of points to the 'W'. After the assignment, it contains the address of the 'A', which is guaranteed to be different.
String literals survive for the life of the program. The assignment does absolutely nothing to the objects being pointed to. The only thing that changes is the contents value of MyString1. It now has the same value as MyString2, namely the address of the 'C'. Whether the four bytes occupied by "Dog" are wasted or not depends on whether something else points to them or can point to them later. This discussion thread is closed Start new discussion.
Fish, Sr. Reset a foreach loop. Resetting Validation Controls via Reset Button. How to perform server-side form reset? Value of variable in my user control keeps getting reset.
Clearing session variables when reset button is pressed. NET Framework. Replies: 2 Last Post: , PM. Classes inheretance problem Replies: 12 Last Post: , PM. Warnings, warnings, warnings? By spentdome in forum C Programming. Replies: 25 Last Post: , PM. All times are GMT The time now is AM. All rights reserved. It's extremely obfuscated though Without a NUL character, it's just an array of characters and must not be passed to functions expecting pointers to strings.
No, in this case you're pointing to a real non-null string with a length of 0. You can simply do the following to set it to actual null:. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Setting a string to null in C Ask Question. Asked 10 years, 9 months ago. Active 10 years, 9 months ago.
Viewed 70k times. Improve this question. This defines a pointer-to-char p1 and a char p2, but it reads as if both p1 and p2 were of type pointer-to-char. Add a comment. Active Oldest Votes.
0コメント