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
Printable View
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?
strcpy(dst,src);
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
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
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
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"...
You need to:
a. Read the tutorial on this site
b. Go to this new web site called Google, search for "strings in c", and actually visit some of the links that are returned from that search and read them.
Sorry, just found it, disregard that last question please. Thanks.
K, so valid question time.
If I usewill other FILE* pointers interfere with that? Because that is all that I can think of that is causing my compiler to "Unexpectedly Quit. Would you like to send an Error Report?" and I don't know what else would be doing it.Code:fgets ( string, 50, stdin );
The %s specification with scanf stops at the first white space character after the first "word".
You can use scanf() to pick up blanks too, but the syntax is a bit different for the format specifier.
See here:
http://www.cppreference.com/stdio/scanf.html
For instance, if you want to pick up all (up to) 80 characters up to and not including the newline, code:
Code:char response[81] ;
scanf("%80[^\n]", response ) ;
If you're using one of the Microsoft compilers, like Visual C++ Express, you should be able to clean from the Build menu, like Build -> Clean Solution, or something similar. Then you just recompile and relink like usual. Other IDEs have this option somewhere, try the help documents.
I got it to work with:Thanks for the help, and I will rebuild if it crashes again.Code:char response[81] ;
scanf("%80[^\n]", response ) ;