#1
  1. No Profile Picture
    matthewdoucette.com
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2002
    Posts
    635
    Rep Power
    13

    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?)
    Matthew Doucette / Xona.com
  2. #2
  3. No Profile Picture
    matthewdoucette.com
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2002
    Posts
    635
    Rep Power
    13
    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.
    Matthew Doucette / Xona.com
  4. #3
  5. *bounce*
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Delft, The Netherlands
    Posts
    514
    Rep Power
    42
    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/
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2001
    Location
    Long Beach, California
    Posts
    86
    Rep Power
    13
    Thanks alot Analyser. Good explination.
  8. #5
  9. No Profile Picture
    matthewdoucette.com
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2002
    Posts
    635
    Rep Power
    13
    Beautiful. Thanks.
    Matthew Doucette / Xona.com
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2002
    Location
    Cape Town SA
    Posts
    3
    Rep Power
    0
    Originally posted by Analyser
    I think the more common term is "string constant"
    Or "string literal" ;)
  12. #7
  13. *bounce*
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Delft, The Netherlands
    Posts
    514
    Rep Power
    42
    *snaps fingers*

    That's the term I was looking for... :)
    "A poor programmer is he who blames his tools."
    http://analyser.oli.tudelft.nl/
  14. #8
  15. No Profile Picture
    matthewdoucette.com
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2002
    Posts
    635
    Rep Power
    13
    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
    Matthew Doucette / Xona.com
  16. #9
  17. No Profile Picture
    matthewdoucette.com
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2002
    Posts
    635
    Rep Power
    13
    I'll crosslink to another thread that helps clear up the situation:
    char* types and cstrings
    Matthew Doucette / Xona.com

IMN logo majestic logo threadwatch logo seochat tools logo