[prog] Strings in C

k.clair kclair at karl.serve.com
Fri Oct 18 09:31:21 EST 2002


Hello,

I don't have an answer, but I do have another question :-D

I've only learned a tiny bit of C, but I was under the impression that
character arrays had to have the \0 character at the end of them in
order to be treated like a string.  Well, this confuses me ... do they
*have* to end with \0, or is it just convenient to have at the end for
working with them?

thanks,
kristina

On Fri, Oct 18, 2002 at 10:30:27PM +1000, Sue Stones wrote:
- below are some quick comments that are hopefully aproximatly correct!
- 
- On Fri, 18 Oct 2002 19:02, Therese Gustafsson wrote:
- > Hi!
- >
- > I'm trying to write a C-program that accesses a
- > postgresql-database. I'm having trouble with the
- > stringhandling and I'm hoping someone here can help
- > me.
- >
- > I have a short code-example and I'm going to try to
- > explain what my problem is.
- >
- > Here are my variables:
- > char email[256],l_email[40];
- > int email_length;
- >
- 
- you could put in a loop here to initialse the string to blanks, or nulls or 
- whatever you want.  Something like this..
- for (i=0; i<email_len; i++)
-  l-email[i] = ' ';
- 
- (whatever the right variales are)
- 
- 
- 
- > This is where the user input is read in.
- > printf("Input email-address: ");
- > fgets(email, 256, stdin);
- >
- > Now here comes my problem. The email field in the
- > database is only 40 characters long, so now I want to
- > make sure that the email string is max 40 characters
- > long and I don't want the "\0"-character at the end of
- > the string.
- >
- > Here's what I've done. I get the length of the string
- > the user input with strlen. (I don't understand why I
- > have to subtract one, but if I don't the stringlength
- > is one character to long. I thought strlen didn't
- > count the "\0"-character?) Then if the length of the
- 
- Usually this is because things are numberd from zero, so teh length is one 
- more than the value of the index.
- 
- > user input is longer than the field in the database I
- > just truncate it.
- > email_length = strlen(email)-1;
- > if (email_length > 40)
- > {
- >         email_length = 40;
- > }
- >
- > Now I copy the user input to another string that's as
- > long as the field in the database. This is to copy all
- > user input except for the "\0".
- > strncpy(l_email,email,email_length);
- >
- > Problem number 1: If the string the user wrote is less
- > then 40 characters the end of the l_email string
- > sometimes contains "junk"-characters. How can I get
- > rid of those? I want the rest of the string empty.
- >
- > Problem number 2: Isn't there an easier way to do
- > this? It seems as if I've made it to complicated.
- 
- I don't know the answer to this one!
- sue
- 
- 
- > I'm sorry if I haven't explained it so you can
- > understand. English isn't my first language.
- >
- > /Therese
- >
- > __________________________________________________
- > Do you Yahoo!?
- > Faith Hill - Exclusive Performances, Videos & More
- > http://faith.yahoo.com
- > _______________________________________________
- > Programming mailing list
- > Programming at linuxchix.org
- > http://www.linuxchix.org/mailman/listinfo/programming
- _______________________________________________
- Programming mailing list
- Programming at linuxchix.org
- http://www.linuxchix.org/mailman/listinfo/programming



More information about the Programming mailing list