Re: Function Problem

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Helge is correct, you *cannot* return pointers to data on the stack and
expect that the data is valid after the return. Either dynamically allocate
a buffer at runtime or modify the original string.

But you have more problems than that.

Isl_Total takes paramters that are pointer to strings of undefined length,
but fnReplace takes pointers to strings of length 16. This inconsistency
could bite you later on when you least expect it.

See below, error checking and freeing is left to you.

extern "C" void _cdecl Isl_Total(char x[], char y[], char *res)
{
char *s = malloc( strlen(y));
char *t = malloc( strlen(x));

strcpy( s, y );
strcpy( t, x );


fnReplace(s);
fnReplace(t);
double xx =atof( s ), yy= atof( t );
double result;

result = xx + yy;

_gcvt( result, 7, res);


}

///////////////////End/////////////////////////////


////////////////Second Function/////////////////////////
void fnReplace (char *RplStr)
{
char find = ',';
char replace = '.';
char string[16];
int len,i;

len = strlen( RplStr );

for (i=0;i<=len;i++)
{
if ( RplStr[i] == find )
RplStr[i] = replace;

}
}
//////////////////////End/////////////////////////////




--
Bruce Eitman (eMVP)
Senior Engineer
beitman AT applieddata DOT net

Applied Data Systems
www.applieddata.net
An ISO 9001:2000 Registered Company
Microsoft WEP Gold-level Member


"Helge Kruse" <Helge.Kruse-nospam@xxxxxxx> wrote in message
news:%23tdqew$SFHA.3312@xxxxxxxxxxxxxxxxxxxxxxx
> Dont know, what "does not work" means, but...
>
> your function fnReplace returns the address of the auto variable "string".
> That doesn't work.
>
> Regards, Helge
>
>


.



Relevant Pages

  • Re: Comments requested: brief summary of C
    ... ``A char holds a single byte. ... ``A string is a contiguous sequence of characters terminated ... pointed to by city. ... pointers and arrays are often used ...
    (comp.lang.c)
  • Re: Can C do it ?
    ... C does not have a native string type. ... has as it's arguments two char pointers. ... Using a struct would be very limiting: ...
    (comp.lang.c)
  • Re: Newbie seeking VB to ANSI C Conversion assistance
    ... >> string manipulation and pointer usage in C. ... >> different seeds and decoding strings. ... > int main(int argc, char *argv) ... > are pointers and since there is no space allocated to store ...
    (comp.lang.c)
  • Re: Can C do it ?
    ... C does not have a native string type. ... has as it's arguments two char pointers. ... But remember, if you misuse it terribly enough, demons /will/ fly out ...
    (comp.lang.c)
  • Re: Reading Zip file contents without DLLs... How?
    ... It will point into some string ... > surrounding code used Delphi long string variables in such a way as ... I'd think it's a sign of delusion about the nature of pointers. ... comparing a Char and a String. ...
    (alt.comp.lang.borland-delphi)