So, do you still get confused about C Pointers? Don't fret, unless you've had a fair amount of practice, it's understandable not to have it all straight. But if you want to get them straight fast, I'd suggest you take a half hour and read this excellent three part article on pointers:
1. http://www.freshsources.com/19930276.HTM
2. http://www.freshsources.com/199302BB.HTM
3. http://www.freshsources.com/199302F2.HTM
Need some C pointer reminders? It's especially good to review them before a major interview if you don't use them every day, since you know they'll probably ask you to reverse a string or something. So here's a quick review:
You take the address of something with &:
// Address of a is:
ptr = &a;
If you've got an address you can "reach into" it with *:
// Contents of ptr is *ptr
a = *ptr;
The only odd case is when declaring a pointer. Declare a pointer to an int as:
int *p;
which is most easily thought of as "*p is an int." So if you reach into p with *, then you'll get an int.
One more time, in rules form:
1. &: Always takes the address of a variable. Mentally read & as "address of."
2. *: There are two cases:
a. Declaring types
b. Returns the contents of the address stored in a variable. Mentally read this as "content of."
Hopefully, you'll never forget your pointers now. By the way, fresh sources has tons of other good in-depth articles that cover C, C++, and Java. You should take a look. And if you're still trying to get comfy with C, but you haven't read and worked through K&R's C Programming Language... umm... what are you waiting for?
Saturday, November 29, 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment