Question concerning pointers

Started by
15 comments, last by Oluseyi 19 years, 3 months ago
You mean 8-bit, and I don't really want to get into the pedantry there...

Anyway. The key thing you need to note is that although individual characters are being assigned, the pointers are being incremented.

Also, a while loop continues until its condition in brackets evaluates false. 0 is false.

Note that the condition uses an assignment, not an equality operator. The assignment will evaluate to the value being assigned, so when 0 is assigned, the loop breaks out.

Note that there is nothing inside the body of the loop.

Now trace through it again, carefully, and understand what it does. Draw yourself a little picture of memory, and put some chars at some locations, and char*'s 'p' and 'q' pointing to them, somewhere else.
Advertisement
Quote:Original post by HughG
Hmmm... A char is an 8 byte value so I am not sure what will happen whenever p or q reaches 255. Does it wrap back around to 0 when it reaches 255? If so then this loop would go on forever...
You're barking up the wrong tree. Remember, pointers contain addresses, and ++ takes precedence over *.
Pointers are a huge topic, and understanding them can take a long period of time. When I was learning C++, I just didn't see the use of them. Indeed, I assumed pointers were totally replacable by regular variables by different implementations.
However, I though functions were just as useless as I didn't see what the problem was with copying and pasting where normal people would call functions :P.

Now, all I can say is hang on there. Just learn them no matter how useless they seem. You can't understand what's the "point of pointers" if you don't know what they are, but you don't really know what pointers are until you learn their point [smile].

Anyways, try to learn a little assembly. You'll immediately understand what is going on under the hood. You'll learn a lot about how memory works in computers. And once you learn how the computer stores stuff, you'll immediately understand what pointers are.

The many uses of pointers are beyond what one could type in a post. I'm not sure how your book is structured, but chances are, you haven't learned classes, or other advanced topics. The more you learn, the more uses you'll see of pointers.
.:<<-v0d[KA]->>:.
Quote:Original post by Zahlman
You mean 8-bit,

Yes, my mistake.

Quote:
and I don't really want to get into the pedantry there...


Huh?

Quote:
Anyway. The key thing you need to note is that although individual characters are being assigned, the pointers are being incremented.

Also, a while loop continues until its condition in brackets evaluates false. 0 is false.

Note that the condition uses an assignment, not an equality operator. The assignment will evaluate to the value being assigned, so when 0 is assigned, the loop breaks out.



Yes I noticed it was assignment. I just thought that no matter what you assigned it to, it would always evaluate to true (E.G. while(a=3) always evaluates to true). I thought if it was a=0 then it would evaluate if a was actually 0 instead of a. Tested it this morning and turns out I was wrong. It's confusing coming from Java where only booleans can be inside the paranthesis.

Quote:
You're barking up the wrong tree. Remember, pointers contain addresses, and ++ takes precedence over *.


Ahh, pointer arithmetic. I'll give it another shot.
Quote:Original post by HughG
Yes I noticed it was assignment. I just thought that no matter what you assigned it to, it would always evaluate to true (E.G. while(a=3) always evaluates to true). I thought if it was a=0 then it would evaluate if a was actually 0 instead of a. Tested it this morning and turns out I was wrong. It's confusing coming from Java where only booleans can be inside the paranthesis.
In C, any non-zero value evaluates as true. What if(a = q) does is first assign the value of q to a, then test the resultant value of a for truth.
char * func(char * p, char * q)
{
while(*p++ = *q++)
;
return p;
}


1. The code copies the character obtained at memory location q into the space at memory location p.
2. Then increments the addresses at q and p by one.
3. Copies the character at q to p.
4. Repeats this process until the nul terminating character at q is reached.
How about them apples?
Correct (step 3 is a repetition of step 1, of course). The effect is to copy a null-terminated character array, the C representation of a string.

This topic is closed to new replies.

Advertisement