[prog] Strings in C

Amanda Babcock alb at quandary.org
Fri Oct 18 10:50:01 EST 2002


On Fri, Oct 18, 2002 at 02:02:19AM -0700, Therese Gustafsson wrote:

> 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.

If you did something along the lines of while n < email_length and
email[n] != \0, then it would copy up to email_length or the end of
the data, whichever comes first.  You would definitely need to initialize
the new string to all blanks first, though, or else it will already
have garbage in it that will show up after the end of the string.

Sorry for not having the above in actual C format... my C is rusty.

Thanks,
Amanda



More information about the Programming mailing list