It's a good beginner question, and the answer involves an important concept.
You have to allocate the memory on the heap instead of on the stack.  This 
means you
allocate the memory dynamically at run-time instead of it being automatically
allocated with the function's activation record.
The malloc function is usually used for this purpose (see "man 3 malloc").

char *strfix = malloc(strlen(str)+1);

malloc'ed memory has to be free'ed though, and for that you need to keep a 
pointer to
it.
So say where you have:
  fprintf( fp, "%s~\n",    fix_string( pMobIndex->long_descr ) );
You can do something like:
  char *tmp;
  ...
  tmp = fix_string(pMobIndex->long_descr);
  fprintf( fp, "%s~\n", tmp);
  free(tmp);

There's the solution.
But in reality, I would just fix the string in-place (overwrite the string with 
the
fixed version).  The strings that are fixed shouldn't have a ~ or \r in them 
anyway,
so fixing them in place shouldn't cause any problems (though the potential for a
small, temporary memory leak exists since you are shortening a string that may 
have
been dynamically allocated with ROM's alloc_mem).
--Palrich.

> Ok I know this is going to sound entirly newbieish, but I tought myself how
> to code, and ammaross' letters take too long to get answered :P So hopefully
> you guys can help :)
> I was wondering if you could do something along the lines of:
> char strfix[sizeof(str)+2];
> Would that work? Or can you not do that with delclarations of variables.
> Thanks for you input ahead of time :)
>
> Sarix


Reply via email to