Link to home
Start Free TrialLog in
Avatar of sa9813
sa9813

asked on

Visual C++ 6.0/Windows XP

The function below runs perfect on Windows 2000 using Visual C++ 6.0. Running the same function on Windows XP professional using Visual C++ 6.0 on the other hand causes an accessviolation! How come, there have to be away around this!

void reverse(char *str){
  int lh, rh;
  char tmp;
  for (lh = 0, rh = strlen(str) - 1; lh<rh; lh++, rh--){
    tmp = str[lh];
    str[lh] = str[rh]; //accessviolation!
    str[rh] = tmp;
  }
}
Avatar of peterchen092700
peterchen092700

the problem is probably the strings you feed - the function will fail with NULL's, and invalid strings.
Check if your string is '\0' terminated
ASKER CERTIFIED SOLUTION
Avatar of pjknibbs
pjknibbs

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
pjknibbs: yes, that's it most likely. Why didn't I think of that....


sa9813:
i.e. even

char * str = "reverseme";
reverse(str);

will fail.

the following should work:

char * str = "reverseme";

char * copy = strdup(str); // create a copy of the string on the heap
reverse(copy);
//...
free(copy);

Avatar of sa9813

ASKER

My problem is now solved. The problem was me, trying to pass a char * like this:

char *p = "reverseme";

which will not work.

I just have a problem deciding who deserves the points! The first comment by peterchen, "- the function will fail with NULL's, and invalid strings", is correct. On the other hand, the one comment who got my attention was the one by pjknibbs. Any comments on who deserves the point?

//sa9813
IMO points should go to pjknibbs, since "invalid strings" is a bit generic
Avatar of sa9813

ASKER

Sorry if it took some time. When trying to accept pjknibbs comment I got an internal server error. Today is probably my lucky day because it works! =) Thanks for your help!