![]() |
| | #1 |
| Registered User Join Date: Apr 2009 Location: ISTANBUL
Posts: 8
| I created this code: -------------------------------------------------- #include<string.h> char a1[ ]="Available"; //and when a condition is provided, I want to change value of a1 to "N/A" -------------------------------------------------- How can I do this? Thanks... |
| yildiz.a is offline | |
| | #2 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,262
| Use a pointer, and just assign it the constant "N/A". Or, use an array that actually has a size, and copy the elements that you need. Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #3 |
| Registered User Join Date: Apr 2009 Location: ISTANBUL
Posts: 8
| Thanks... |
| yildiz.a is offline | |
| | #4 |
| Registered User Join Date: Apr 2009 Location: Russia
Posts: 116
| you can use only Code: char a1[ ]="Available"; |
| c.user is offline | |
| | #5 | |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| Quote:
You (c.user) should for your own good explain why you believe this. Look: Code: #include <stdio.h>
#include <string.h>
int main() {
char a1[]="Available";
strcpy(a1,"NONSENSE");
printf("%s\n",a1);
return 0;
}
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS Last edited by MK27; 04-27-2009 at 07:55 PM. | |
| MK27 is offline | |
| | #6 |
| Registered User Join Date: Apr 2009 Location: Russia
Posts: 116
| you may compile this, there are no errors ![]() Code:
main()
{
char a[] = "Available";
return 0;
}
Code:
#include <stdio.h>
/* change value of array to another */
main()
{
char a[] = "Available";
const char *n = "N/A";
unsigned len;
sprintf(a, "%.*s", (len = sizeof a) > 0 ? len-1 : 0, n);
printf("%s\n", a);
return 0;
}
![]() oh, the program doesn't need it update: '\0' will be added Last edited by c.user; 04-27-2009 at 09:28 PM. |
| c.user is offline | |
| | #7 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,262
| Not all string functions are in the string header. Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #8 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| This is a fine option so I won't hold you up to a firing squad, but it still does not make sense of what you said before, which was that "you can only use [this] if you are not using string.h".
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS |
| MK27 is offline | |
| | #9 | |
| Registered User Join Date: Apr 2009 Location: Russia
Posts: 116
| MK27, Quote:
string - is a chars + '\0' string function - is a function for string which has '\0' Last edited by c.user; 04-27-2009 at 09:39 PM. | |
| c.user is offline | |
| | #10 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,262
| Any time you use an incomplete array, and declare it with an assignment from a string in quotes, it automatically has room for the null character. Because you've just used a string in quotes... Therefore, assuming you don't run off the end of the array's size there, most string functions will work just fine. Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #11 |
| Registered User Join Date: Apr 2009
Posts: 16
| Why not just create two char arrays? Code: char a1[] = "Available"; char a2[] = "NotAvailable"; And if your worried about the space just have one Code: char a2[] = "NotAvailable"; just start printing the char array from Code: a2[3] until '\0' Just do Code: if(condition)
{
a1[0] = 'N';
a1[1] = '/';
a1[3] = 'A';
a1[4] = '\0';
}
But you cant stuff more stuff into the array without deleteing what was already there unless you made the array large enough to hold both to start with. Last edited by strictlyC; 04-28-2009 at 03:11 PM. |
| strictlyC is offline | |
| | #12 | |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,262
| Quote:
Code: printf( "%sAvailable", available ? "" : "Not " );
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? | |
| quzah is offline | |
| | #13 | |
| Registered User Join Date: Apr 2009 Location: Russia
Posts: 116
| Quote:
Code: char a[5] = "abcde"; and strcpy will continue copying while it will not get a '\0' and Code: #include <string.h>
...
char a_mess[] = "Available";
strcpy(a_mess, a);
(a[5] like a sixth element of array a) Last edited by c.user; 04-28-2009 at 03:46 PM. | |
| c.user is offline | |
| | #14 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,262
| It wasn't. That was the whole point. Which has nothing to do with the original post or the associated comment. No one was saying "Hey, if you use a malformed array as a string and try to use string functions over top of this array, it's not going to work!" Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #15 |
| Registered User Join Date: Apr 2009
Posts: 41
| |
| strickyc is offline | |
![]() |
| Tags |
| string |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Need help with comparing strings (I'm a beginner C programmer) | Bandizzle | C Programming | 2 | 04-29-2009 10:13 AM |
| beginner question about strings | gp364481 | C Programming | 4 | 09-05-2008 06:31 PM |
| beginner: dynamic array of strings | pc2-brazil | C++ Programming | 10 | 04-29-2008 04:29 PM |
| Strings Program | limergal | C++ Programming | 4 | 12-02-2006 03:24 PM |
| A beginner passing strings | jamjar | C Programming | 2 | 09-01-2002 12:50 AM |