I want my string (s) to store "gold". Why does this not work?:
ThanksCode:#include<stdio.h> int main() { char s[5]; s = "gold"; printf("%s", s); getchar(); }
-Matt
I want my string (s) to store "gold". Why does this not work?:
ThanksCode:#include<stdio.h> int main() { char s[5]; s = "gold"; printf("%s", s); getchar(); }
-Matt
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.
Okay, so what would the syntax for that be?
One thing you can do is
The above is resolved at compile time. A runtime, you have to use runtime functions.Code:char s[] = "gold" ;
Todd
Mainframe assembler programmer by trade. C coder when I can.
Thanks, but I need to assign it later on in the program.
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.
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); }
Sorry, also wanted to add then later try to assign like so
The tricky part is that this compiles but is wrong!Code:*sa = "I need more gold";
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++.
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";
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.
Lol, what I wrote was intended for the O/P only as an exercise and to point out potential pitfalls.
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.
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.
New question about strings; How do I put info into a string withoutAnd why does that not pick up anything after a space?Code:scanf("%s"...