Re: Way around error?
From: Balboos (balboos_at_masonicbrother.com.No.Spam)
Date: 05/03/04
- Next message: Johan Rosengren: "Re: MRU file crashes"
- Previous message: Adrian Gibbons: "Re: MRU file crashes"
- In reply to: Balboos: "Way around error?"
- Next in thread: Joseph M. Newcomer: "Re: Way around error?"
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 03 May 2004 10:19:55 GMT
Thank you both for the confirmation that it's just not going to happen.
The hope was to design a method that was 'universal' - since the string
never changes length, this would be possible safely "in theory." It
shouldn't be needed for const char * (as initialized for failure), since
the programmer should create the constant correctly.
Even in 'C', you can't have it all;
Thanks, again,
Balboos
Balboos wrote:
> Hi, all.
>
> I'm making a number of function overloads for a 'capitlizing' function,
> it's purpose to convert an input string to mixed upper and lower case as
> though it were (for example) a name. The (char *) version's giving me
> some grief. The code (below) works if the string is allocated from the
> heap, but not from the stack (access violation).
>
> i.e., if I use:
> char instring[9] = "john doe";
> I get back "John Doe".
>
> If I use
> char *instring = "john doe";
> I get the access violation at the first attempt to modify the string.
> Similarly, if I call the function with the text as the argument.
> i.e., Capitalize("john doe"), it crashes.
>
> // SOURCE
> char *Capitalize(char *instring) {
>
> char *tmp; // for copy of pointer to input char string.
> bool needUC=true; // set if we 'need' to make next alpha UC; else LC
>
> if(instring!=NULL) {
> strlwr(instring); // Start out all LC - ACCESS VIOLATION!
> for(tmp=instring; *tmp; ++tmp) {
> if(!isalpha(*tmp)) {
> needUC = true; // We've hit a non-alpha - set UC flag
> continue;
> } // if(!isalpha(*tmp))
> if(needUC) {
> *tmp = toupper(*tmp);
> needUC = false; // set flag that UC is satisfied
> } // if(needUC)
> } // while(tmp=instring; *tmp; ++tmp)
> } // if(instring!=NULL)
>
> return instring;
> } // char *Capitalize(char *instring)
>
> Is there a way around this?
>
> Balboos
>
- Next message: Johan Rosengren: "Re: MRU file crashes"
- Previous message: Adrian Gibbons: "Re: MRU file crashes"
- In reply to: Balboos: "Way around error?"
- Next in thread: Joseph M. Newcomer: "Re: Way around error?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|