swap function (debugged in VS Express)

I tried to debug this function in Visual Studio Express 2004
http://paste.ofcode.org/EnJsc5iJ7BFmPBCP7Tg86Y

I would like to understand how it works. Maybe I already found it, hut there is not something clear during the debuging in VS.

Here are the notes that I added to the code:
1
2
3
4
5
6
	while ((*a != '\0') && (*b != '\0'))
	{
		temp = *a; // 1) set first character of variable "a" to temp
		*a++ = *b; // 2) set first character of variable "a" to first character of variable "b" and then increase array pointer +1
		*b++ = temp; // 3) set b to temp and then increase array pointer +1
	}


Original state: a="world", b="hello ",*a="w",*b="h"

I added breakpoint to while loop and then I debugged it line by line. I thought I should see the a or b how it changes to a="horld", b="wello" but it erases characters from the string (this is how it looks to me). So it looks like so: a="orld", b="ello " (watching a and watching b).

Now watching *a and *b: *a="o", *b="e".

Do you have idea why I cannot see the complete string in the time how it is changed? I see only the result on the cout line.
char * p = "asd";

*p -> asd\0
*(p+1) -> sd\0
*(p+2) -> d\0
*(p+3) -> \0

so this swap is changing 1 sign at a time. If you want to see this change watch r1 and r2
I see. But I don't see any value to r1 or *r1 while I am in the while loop in the function.
then for the testing purpose make r1 and r2 global and then watch their values.

Or just use data dump and track c string address.
I declare global like this:
char *r1, *r2;
implementation:
char *r1 = _strdup("world"), *r2 = _strdup("Hello ");
watching during the while loop:
r1 = 0x00000000 <Bad Ptr>
1
2
3
4
5
6
7
8
9
10
11
char *r1 = strdup("world"), *r2 = strdup("Hello ");	

void swap(char *a, char *b)
{
	register char temp; 
//breakpoint here
	while ((*a != '\0') && (*b != '\0'))
[...]
} 

[...]


and it is working like you want
Topic archived. No new replies allowed.