Hello Everyone,

I found the following code somewhere, I understand it but I have trouble with understanding the sequence of execution, in other word, How can I monitor every little step of the function like that?

void EatSpace(char* Pbuffer)
{
	int i = 0;
	int j = 0;
	while((*(Pbuffer + i) = *(Pbuffer + j++)) != '\0')
		if (*(Pbuffer + i) != ' ')
			i++;
}

Recommended Answers

All 13 Replies

Means..
n=0, m=0
1. if Buffer[n] = 'null' then stop
2. if Buffer[n] = 'space' then n = n + 1, m = m + 1, back to step 1
3. m = m + 1
4. Buffer[n] = Buffer[m], back to step 1

>>How can I monitor every little step of the function like that?

1) use a debugger

or
2) take a short test string and follow it manually. For example: "H W". Initially variables i and j are both 0, so it will just copy the character 'H' onto itself, then increment j so that j points to the space. Next it asks if i points to a space, and if it does not then incremenet i.

Second loop iteration: i and j are both 1 and both point to the space. So it will copy the space onto itself, then ask if i points to space. It does in this case, so it will NOT increment i.

Third loop iteration: i = 1 and j = 2 so it copies the 'W' onto the space. j is incremented to 3 and since the character at i is now a 'W' instead of a space the value of i is incremented to 2.
The string now looks like this: "HWW"

Fourth loop iteration: i = 2 and j = 3. The string's null terminator is copied to the second 'W' in the string so that the string is contains this: "HW". The whole thing stops because it just copied the '\0' character.

Mr,Ancient Dragon
you said:
>>>>Next it asks if i points to a space, and if it does not then incremenet i.

how can you decide it, why not j?

When I want to thoroughly test something I take it one TINY step at a time, but that might have a lot to do with my OCD :D

I would worry about the first step, ensure that works fine, then move on to testing the rest of them, rather than trying to monitor everything all at once.

I can break the execution of the following code before the compiler starting to execute it or after execution of it.
But the line contains many steps, I simply want to trace it, How?

while((*(Pbuffer + i) = *(Pbuffer + j++)) != '\0')

Mr,Ancient Dragon
you said:
>>>>Next it asks if i points to a space, and if it does not then incremenet i.

how can you decide it, why not j?

if (*(Pbuffer + i) != ' ')
			i++;

Read the code! Does the code say j? NOOOOOO

I can break the execution of the following code before the compiler starting to execute it or after execution of it.
But the line contains many steps, I simply want to trace it, How?

while((*(Pbuffer + i) = *(Pbuffer + j++)) != '\0')

First do whatever is inside the parentheses, then do the rest.

1) calculate the address Pbuffer+i -- increments pointer Pbuffer by the value of i, and store that value somewhere.

2) calcualte Pbuffer + j and saves that someplace else.

3) increment j

4) copy the character at the pointer calculated in 2) above to the address calculated by 1) above. If i and j had the same value then both 1) and 2) would result in the same address.

5) if the character at the address calculated in 2) above is '\0', then stop the loop.

Don't give me a fish, teach me how to fish.

Don't give me a fish, teach me how to fish.

Just trying to answer your question. If you don't like the answer I gave you, then I'll shut up.

Mr, Ancient Dragon
take it easy, I don't mean that, I just wanted to know how can I make it with myself.

Like I said before, do whats in the inner-most parentheses first, then work yourself out. Once you have removed all parentheses, work the statement from left to right.

For example: a - ((b * c) / d) + e; First calculate b*c, then divide that result by d, finally subtract that result from the value of a and add e.

f = b * c;
now the equation will look like this: a - (f / d) + e g = f / d; ===> a - g + e

Thank you for you and everyone.

Anciant Dragon has given the answer of question you've asked, you asked for What is the Execution Sequence you didn't ask How to find out the execution sequence.

Let me teach you how to find it out.

void EatSpace(char* Pbuffer)
{
	int i = 0;
	int j = 0;
	while((*(Pbuffer + i) = *(Pbuffer + j++)) != '\0')
		if (*(Pbuffer + i) != ' ')
			i++;
}

when the above funciton is called, Compilers will push the PTR Puffer on stack then push the Local variables i and J on the stack.
now the interesting part. while((*(pbuffer+i) = *(pbuffer + j++)) != 0) assignement operator are evaluatted from right to left.
so the above code can be broken as follows *(pbuffer + j++) the above expression is evaluated as follows

take the address of pbuffer add j to its contents and derefer the complete value. increment the j for next execution

you will get some character in that case aren't you?
assign that character to lvalue which is *(pbuffer + i) you can break the above statement now,
lets say right value calculated as 'C' you assign it to left handside so the right handside become 'C' as well which is not equal to '\0'
isn't it
always remember one thing there is still a world ahead
a more better explanation can be provided to you but the thing is that you don't know the assembly and compilers. Sometimes experts like Anciant Dragon doesn't explain the very minor details because either they are irrelevant to you or more advance so that you can't grasp them. be patient just take a hint and dig it out yourself one day you'll learn why Anciant doesn't tell you the complete story.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.