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';
Printable View
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.
Or maybe you could do something with pointers!
Code:#include <stdio.h>
int main()
{
char *string;
scanf("%s", &string);
printf("%s", &string);
return 0;
}
OMFG No! You're joking right?! Please tell me you're joking!Quote:
Originally Posted by xxxrugby
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!
xxxrugby, your code gave me hives.
Since everyone likes a helpful reply, here's mine:Quote:
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.
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"
>>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.
Please don't act like you know what you're talking about.Quote:
Originally Posted by xxxrugby
Quzah.
This is likely because of the first point quzah made, i.e.:Quote:
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.Quote:
Originally Posted by quzah
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;
}
strcpyQuote:
Originally Posted by nizbit