![]() |
| | #1 |
| Registered User Join Date: Feb 2008
Posts: 12
| Initializing arrays Code: char s[]="abc"; Code: char s[8]; s[]="abc"; The reason I would want the second alternative is lets say I have conditions with which I want to apply to s[]. Depending on the condition it will house a different string(none of which are longer than the array, including \0 of course), I would then want to print s[]. How would I get something like that to work? Last edited by bwisdom; 11-07-2009 at 11:35 AM. |
| bwisdom is offline | |
| | #2 |
| Jack of many languages Join Date: Nov 2007 Location: Katy, Texas
Posts: 2,071
| You have to use strcpy() in C (or an equivalent function). In C, you can't just assign a string to a string variable. (edit: unless you are declaring it, and then that action happens at compile time, not runtime)
__________________ Mac and Windows cross platform programmer. Ruby lover. Last edited by Dino; 11-07-2009 at 11:36 AM. |
| Dino is offline | |
| | #3 | |
| The larch Join Date: May 2006
Posts: 3,222
| Because initializing only works where you declare things. Arrays cannot be assigned later. So look into strcpy, to set the string contents later.
__________________ I might be wrong. Quote:
| |
| anon is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Game of life: Initializing a struct with two 2D arrays | Brafil | C Programming | 23 | 12-06-2008 01:39 PM |
| newbee: initializing arrays | breaka | C Programming | 11 | 06-12-2006 12:20 PM |
| Initializing dynamic arrays | cunnus88 | C++ Programming | 9 | 11-21-2005 09:50 AM |
| initializing values for arrays, and chaging them w/o using scanf | sly_ | C Programming | 14 | 03-17-2003 10:07 AM |
| initializing char arrays to null | swiss powder | C++ Programming | 6 | 02-28-2002 02:56 PM |