Re: problem manipulating strings

From: BRiTiSH (clisowsk@mcs.kent.edu)
Date: 08/02/95


> 
>    >> void set_title(struct char_data * ch, char *title)
>    >> {
>    >>   if (title == NULL) {
>    >>     title = READ_TITLE(ch);
>    >>     if ((GET_CLASS(ch) == CLASS_PALADIN) && (GET_LEVEL(ch) == 10)
>    >>      /* this next line causes the Segmentation fault */
>    >>       strcat(title, "some string");
>    >i'm not a "rocket scientist" at C but i dont think strcat will work for 
>    >this. if the pointer is null (there is no data inteh string) then you 
>    >cant ADD to it. you would want to use:
>    >   strcpy(title, "some string");
>    >and then if you want to append to title, use strcat
> First of all, yes, the parentheses are screwed on the 5th line (only in
>  my example, though, not in the source). But, I am not trying to strcat to 
> a null string, I don't think...  title has the value READ_TITLE(ch) if you 
> look a little more closely.  It already has a string, and I don't think the 
> length of title is formally declared, but when this gets called in the
> advance() function, the *title arg is passed as NULL.  There is no prob. with
> assigning title a value of length strlen(READ_TITLE(ch)), so why is there
> a prob. adding an extra "some string" on to it??

	First of all you are trying to copy the data from the "some string"
into the area of memory pointed to by 'title'. Title might only point
to a spot in memory that is only 2 bytes long or shorter than the string
you are trying to print into it. For all that I know, title might as well
be pointing to a read-only segment of memory, most likely to the default
title strings defined somewhere in the code.
	My suggestion is to use something such as:
	title = strdup("some string");

--Craig



This archive was generated by hypermail 2b30 : 12/18/00 PST