|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
strings vs. constant strings (what's the difference?)
Can someone please explain the difference of strings and constant strings?
(My guess is that constant strings are hardcoded and strings are not. For example, in printf("Hello World!");, "Hello World!" is a constant string. Am I right?) |
|
#2
|
|||
|
|||
|
Another thing,
When using the strcpy function, for example, which is defined like this: - char *strcpy(char *dest, const char *src); What does "const char *src" mean? I know that you can pass in what I believe to be a constant string like this: - strcpy(name,"Matthew"); And I believe you can also pass in a pointer to an array of char (null terminated) like this: - char *entered_name; - strcpy(name,entered_name); ..so what does "const char *src" mean altogether? Last edited by Doucette : February 3rd, 2003 at 01:09 PM. |
|
#3
|
||||
|
||||
|
I think the more common term is "string constant" (as opposed to, say, an integer constant), but what's in a name?
![]() Some examples: char hello[] = "Hello world!"; char *bye = "Goodbye, cruel world!"; In the above, "Goodbye, cruel world!" is a string constant, and is stored in the text segment in the program's object code. That means you can't change it; you'll get a segmentation fault. The actual variable bye on the other hand, is a pointer, and gets 4 bytes (on most machines, anyway) in the data segment, and is initialised to point to the string in the text segment. So you can still change the value of bye, but not the string that it initially points to. With hello, the string is stored in the data segment, where you can modify it to your heart's content ![]() Finally, about strcpy and the meaning of const in its prototype: Code:
char *strcpy(char *dest, const char *src); const is a safeguard; a hint to the compiler, and to the programmer. Basically, when you declare a function's parameter as being constant, you imply that the function will not change the parameter's value. And with strcpy(), that makes sense. When you make a call to strcpy(), you don't expect it to change the source string's contents. And if you did put code in the function's body that modifies the value, the compiler can issue a warning, or error or whatever.
__________________
"A poor programmer is he who blames his tools." http://analyser.oli.tudelft.nl/ |
|
#4
|
|||
|
|||
|
Thanks alot Analyser. Good explination.
|
|
#5
|
|||
|
|||
|
Beautiful. Thanks.
|
|
#6
|
|||
|
|||
|
Quote:
Or "string literal" ![]() |
|
#7
|
||||
|
||||
|
*snaps fingers*
That's the term I was looking for... ![]() |
|
#8
|
|||
|
|||
|
Declaring and initializing values of variables causes a bit of the confusion. Let me see if I have this right:
Original Code: ----------------------- char hello[] = "Hello world!"; char *bye = "Goodbye, cruel world!"; ----------------------- 1st line: not need to know length in this method. "Hello world!" probably exists in text segment somewhere (or at least in the compiled code) but now it exists in the data segment for sure. 2nd line: creates a pointer. point pointer, bye, to "Goodbye, cruel world!" that exists in text segment Declaring and Initializing Separated Code: ----------------------- char hello[13]; strcpy(hello,"Hello world!"); char *bye; bye = "Goodbye, cruel world!"; ----------------------- 1st line: must know length in this method, that's why I put 13 (length of string plus null character). 2nd line: full array copy. "Hello world!", again, probably exists in text segment somewhere (or at least in the compiled code), but now it exists in the data segment for sure. 3rd line: creates a pointer 4th line: point pointer, bye, to "Goodbye, cruel world!" that exists in text segment |
|
#9
|
|||
|
|||
|
I'll crosslink to another thread that helps clear up the situation:
http://forums.devshed.com/showthrea...&threadid=54168 |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > strings vs. constant strings (what's the difference?) |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|