Re: [SLUG] evil C question! AAAGGHH!! help !!

From: Jason Copenhaver (jcopenha@csee.usf.edu)
Date: Wed Jun 20 2001 - 08:21:07 EDT


You should be using a strcpy(or strncpy ) here.. otherwise you set your
PORT string to point to someplace in your line string.. which I'm assuming
is a local variable and will probably get overwritten when doing function
calls.. try a

just be sure that before this you have called

comm_settings.PORT = malloc(sizeof(char),SIZE);

somewhere before you decide to use PORT

and free(comm_settings.PORT) somewhere when you are done.

strncpy(comm_settings.PORT,++equal_ptr,SIZE);
where SIZE is max size of comm_settings.PORT minus one.. then set the last
char to null to make sure you are null terminated..

On Tue, 19 Jun 2001, Glen wrote:

>
> In another function, parse_comm_config():
>
> <snip>
> if(strstr(line, "PORT")){
> equal_ptr = strchr(line, '=');
> comm_settings.PORT = ++equal_ptr;
> comm_settings.PORT[strlen(comm_settings.PORT)-1] = '\0';
> equal_ptr = NULL;
> continue;
> }
> <snip>
>
> line[] is a char * to a line of text read in from a config file with
> fgets(). It's read in just fine here, as shown when I print it with fprintf()
> from the calling function after this one returns. The char *equal_ptr is
> nullified after each line is parsed for safety's sake (i.e. I did a stupid
> oops later on in the code with this).
>
> Glen
>
>
> On Tuesday 19 June 2001 21:45, you wrote:
> > where and how do you set the PORT character pointer?
> >
> > Ed.
> >
> > Glen wrote:
> > > Dear list,
> > >
> > > I have been having trouble with something very odd. Here is my
> > > global struct:
> > >
> > > struct{
> > > int BAUD;
> > > int PARITY;
> > > int DATA;
> > > int STOP;
> > > int FLOW;
> > > int FLAGS;
> > > char *PORT;
> > > }comm_settings;
> > >
> > > When I call a certain function, the char * variable in the struct gets
> > > killed. Observe:
> <snip>
>



This archive was generated by hypermail 2.1.3 : Fri Aug 01 2014 - 19:23:35 EDT