C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 04-27-2009, 03:23 PM   #1
Registered User
 
Join Date: Apr 2009
Location: ISTANBUL
Posts: 8
Question Beginner help about strings

Hi,

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   Reply With Quote
Old 04-27-2009, 03:32 PM   #2
+++ OK NO CARRIER
 
quzah's Avatar
 
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   Reply With Quote
Old 04-27-2009, 03:46 PM   #3
Registered User
 
Join Date: Apr 2009
Location: ISTANBUL
Posts: 8
Thanks...
yildiz.a is offline   Reply With Quote
Old 04-27-2009, 07:30 PM   #4
Registered User
 
Join Date: Apr 2009
Location: Russia
Posts: 116
you can use only
Code:
    char a1[ ]="Available";
if you are not use any functions from string.h like strcpy
c.user is offline   Reply With Quote
Old 04-27-2009, 07:43 PM   #5
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
Quote:
Originally Posted by c.user View Post
you can use only
Code:
    char a1[ ]="Available";
if you are not use any functions from string.h like strcpy
Even considering your english is not perfect (that is OK with me), this is NONSENSE. Not true.

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   Reply With Quote
Old 04-27-2009, 09:05 PM   #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;
}
where is string.h ?
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   Reply With Quote
Old 04-27-2009, 09:08 PM   #7
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,262
Quote:
Originally Posted by c.user View Post
where is string.h ?
oh, the program don't need it
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   Reply With Quote
Old 04-27-2009, 09:20 PM   #8
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
Quote:
Originally Posted by c.user View Post
you may compile this, there are no errors
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   Reply With Quote
Old 04-27-2009, 09:34 PM   #9
Registered User
 
Join Date: Apr 2009
Location: Russia
Posts: 116
MK27,
Quote:
Originally Posted by my
any functions from string.h
if he does not use any function from string.h, he does not need string.h in the program

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   Reply With Quote
Old 04-28-2009, 02:37 PM   #10
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,262
Quote:
Originally Posted by c.user View Post
string function - is a function for string which has '\0'
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   Reply With Quote
Old 04-28-2009, 03:08 PM   #11
Registered User
 
Join Date: Apr 2009
Posts: 16
Why not just create two char arrays?
Code:
char a1[] = "Available";
char a2[] = "NotAvailable";
And have your condition just call the one thats needed?
And if your worried about the space just have one
Code:
char a2[] = "NotAvailable";
If you need to print Available
just start printing the char array from
Code:
a2[3] until '\0'
If your just wanting to change the contents of the array that contains Available

Just do
Code:
if(condition)
{
a1[0] = 'N';
a1[1] = '/';
a1[3] = 'A';
a1[4] = '\0';
}
The rest of the array wont matter.
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   Reply With Quote
Old 04-28-2009, 03:10 PM   #12
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,262
Quote:
Originally Posted by strictlyC View Post
Why not just create two char arrays?
Code:
char a1[] = "Available";
char a2[] = "NotAvailable";
And have your condition just call the one thats needed?
Or...
Code:
printf( "%sAvailable", available ? "" : "Not " );
Quzah.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 04-28-2009, 03:43 PM   #13
Registered User
 
Join Date: Apr 2009
Location: Russia
Posts: 116
Quote:
Originally Posted by quzah
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.
If array is complete
Code:
    char a[5] = "abcde";
there will no '\0' in it

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);
can cause segfault because a[5] may be a trash (not '\0')

(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   Reply With Quote
Old 04-28-2009, 03:49 PM   #14
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,262
Quote:
Originally Posted by c.user View Post
If array is complete
It wasn't. That was the whole point.
Quote:
Originally Posted by c.user View Post
and strcpy will continue copying while it will not get a '\0'
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   Reply With Quote
Old 04-28-2009, 04:22 PM   #15
Registered User
 
Join Date: Apr 2009
Posts: 41
Quote:
Originally Posted by c.user View Post
If array is complete
Code:
    char a[5] = "abcde";
there will no '\0' in it
I dont think anyone said anything about initializing the array like that, not even the original poster.
strickyc is offline   Reply With Quote
Reply

Tags
string

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 06:16 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22