Thread: String assignments

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    65

    String assignments

    I want my string (s) to store "gold". Why does this not work?:
    Code:
    #include<stdio.h>
    
    int main()
    {
        char s[5]; 
        s = "gold";
        printf("%s", s);
        getchar();
    }
    Thanks
    -Matt

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    s is an array, and cannot be used as a modifiable lvalue. IOW, you can't change where s points to, it must always point to the five characters set aside when it was declared. If you want to copy a string into those characters, you must use strcpy.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    65
    Okay, so what would the syntax for that be?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    strcpy(dst,src);

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    One thing you can do is
    Code:
    char s[] = "gold" ;
    The above is resolved at compile time. A runtime, you have to use runtime functions.

    Todd
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    65
    Thanks, but I need to assign it later on in the program.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, you either need strcpy(), or make it a char *str;

    You use strcpy() to copy a string from (say) a temporary location to another array. This is useful if you have some data that will "disappear" when the function finishes, or you need to later on modify the content without changing the original string.

    You use char *str if you already have a (permanent) string that you just need to remember. For example if you have an input as a number (error code, for example) and you want to translate that to a string, there's no need to COPY the string, you just need the address of the existing error message. Note that string literals are constant data, so you can not modify the string.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    This is simple enough code. Play around with your array sizes for sa.


    Code:
    int main(int argc, char** argv) {
        
        char *s = "gold";
        char sa[11] ={"gold_array"};   /*watch out here, test with array size of 10! */
        
        printf("%s\n", s);
        printf("%s\n", sa);
        
        return (EXIT_SUCCESS);
    }

  9. #9
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Sorry, also wanted to add then later try to assign like so

    Code:
    *sa = "I need more gold";
    The tricky part is that this compiles but is wrong!

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by slingerland3g View Post
    Sorry, also wanted to add then later try to assign like so

    Code:
    *sa = "I need more gold";
    The tricky part is that this compiles but is wrong!
    It's very wrong, because you're dereferencing the pointer, thus it becomes a char. You will get a big fat warning about that in C and won't compile in C++.

    Quote Originally Posted by slingerland3g View Post
    This is simple enough code. Play around with your array sizes for sa.
    Don't test with a size of 10. 10 is not enough to hold the buffer and that will result in undefined behavior and buffer overrun if the compiler will allow it.
    http://cpwiki.sf.net/Buffer_overrun

    Plus
    char *s = "gold";
    Should be
    const char* s = "gold";

    Then this is perfectly fine:
    sa = "I need more gold";
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    Elysia, I think that's why he was saying to test it out. To see for himself. Because it doesn't work. Or rather, it compiles, but doesn't do what you want and is terrible programming practice.

  12. #12
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Lol, what I wrote was intended for the O/P only as an exercise and to point out potential pitfalls.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by JDGATX View Post
    Elysia, I think that's why he was saying to test it out. To see for himself. Because it doesn't work. Or rather, it compiles, but doesn't do what you want and is terrible programming practice.
    Though I'm afraid it might just compile. C, for example, allows for non-NULL terminated strings, thus it does compile.
    (Using Visual Studio and C to compile that, it actually compiles.)
    OK so it doesn't create a buffer overrun, but no string utility will work and then will DO buffer overruns when manipulating the strings. Bad.
    Last edited by Elysia; 04-15-2008 at 01:48 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Unfortunately, such advice causes http://cpwiki.sourceforge.net/Undefined_behaviour - and it's by far not defined to cause a crash - it may just silently work in some machines, very much depending on what is after the array - particularly if it's a global variable, since they are automatically filled with zeros.

    To PROVE that something goes wrong, you would have to make some more complex construct, and then ensure that things like alignment "holes" and other similar things aren't causing it to work anyways - this is one of the BIG problems with code that contains undefined behaviour - it may well work on 8 out of 10 combinations of compiler and processor, only 2 of the ten combinations fail...

    Instead, explain that it's bad to do such things.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Registered User
    Join Date
    Mar 2008
    Posts
    65
    New question about strings; How do I put info into a string without
    Code:
    scanf("&#37;s"...
    And why does that not pick up anything after a space?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM