I can't seem to figure out how to set a string array equal to a particular character string. For example:
I know the above is not valid, but that is what I'm trying to do.Code:char yourname[40]; yourname = 'Will Ferrell';
I can't seem to figure out how to set a string array equal to a particular character string. For example:
I know the above is not valid, but that is what I'm trying to do.Code:char yourname[40]; yourname = 'Will Ferrell';
Check out the strcpy() function.
If you understand what you're doing, you're not learning anything.
Or maybe you could do something with pointers!
Code:#include <stdio.h> int main() { char *string; scanf("%s", &string); printf("%s", &string); return 0; }
Sorry for spelling errors, not English!
xxxrugby: "All Human Race Will Die From My Hand!"
xxxrugby: "We are all philosophers, when question is about politics!"
OMFG No! You're joking right?! Please tell me you're joking!Originally Posted by xxxrugby
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
Ups. I think that he wont something else. Sorry. I now take a dictionary and see that my translation was not wery good!
I translate that he want a string of let say five char. And then the array is make by five places, after the string was insert!
My mistake!
Sorry for spelling errors, not English!
xxxrugby: "All Human Race Will Die From My Hand!"
xxxrugby: "We are all philosophers, when question is about politics!"
xxxrugby, your code gave me hives.
If you understand what you're doing, you're not learning anything.
Since everyone likes a helpful reply, here's mine:Originally Posted by xxxrugby
1) Here you declare a pointer. Where is it pointing now? In other words, your pointer has never been set to point at anything, so it's pointing at some random spot in memory. It has no memory allocated for anything other than the pointer itself.Code:#include <stdio.h> int main() { char *string; /* 1 */ scanf("%s", &string); /* 2 */ printf("%s", &string); /* 3 */ return 0; }
2) Here you scan into where the pointer is pointing. Where is it pointing again? Oh, also, your scanf call is wrong. You don't need to give the address-of the pointer, because scanf expects a pointer, not the address of a pointer. So remove the & when you're using a pointer already.
3) Your printf call is also wrong. Even if this weren't a pointer, you wouldn't use the address-of operator. You never use the address-of operator with printf, unless you are in fact trying to display the address-of a variable.
Quzah.
Hope is the first step on the road to disappointment.
Yes you need in scanf and printf &
If I remove them then I get
Code:The instruction at "0x78...." referenced memory at "0x00....". This memory could not be "read"
Sorry for spelling errors, not English!
xxxrugby: "All Human Race Will Die From My Hand!"
xxxrugby: "We are all philosophers, when question is about politics!"
>>Yes you need in scanf and printf &
>>If I remove them then I get
You missed the point. It's broken either way if the pointer doesn't point to anything except a random address.
Kampai!
Please don't act like you know what you're talking about.Originally Posted by xxxrugby
Quzah.
Hope is the first step on the road to disappointment.
This is likely because of the first point quzah made, i.e.:Originally Posted by xxxrugby
You need to malloc some space for the string pointer to point to before you start saving data to that location.Originally Posted by quzah
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
Just to clear anything up, I'm not trying to get input from the user. I have the string that should be put in the array. I know you can't put a string into an array with a simple assisgnment statement. My question is then, how do you do it?
Actually,
This is what is known as a string literal, and the compiler automatically assigns the terminating character \0 to the array.Code:#include <stdio.h> int main(void){ char myString[] = "I can do this"; printf("%s\n", myString); return 0; }
i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced
It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah
Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem