| |||||||||||
| |||||||||||
Pointers in C
I know when I'm giving a C Programming course that I'll come to the section on pointers and I'll have to slow right down - those extra * and & characters take a little explaining and getting used to.
Here are the key points. A variable declared with a * in front of it holds a reference to (i.e. the address of) another variable. The type of the OTHER variable is stated inthe declaration. Thus: float *boat; declares a variable called boat that holds an ADDRESS, and at that address you'll find a float variable. A reference to a variable preceeded with an & calls up the ADDRESS of a variable rather than its contents - so it's suitable for assigning to a variable of the type described in the previous paragraph. And a reference to a variable preceeded with an * calls up the CONTENTS of the address pointed to by the variable - in effect it's the opposite of &. Example: int main(int argc, char ** argv) {WHY? Because you can assign lots of different variable addresses in turn to a single pointer variable, teh use common code to process a whole lot (array) of data. You'll note that I've illustrated this in the example above by exactly duplicating two of the lines of code, though in a real life application I would prefer not to duplicate but rather to write them into a separate function. Let's run that code and see when Bill and Ben retire ... [trainee@daffodil cxx]$ gcc -o panda panda.c(written 2007-04-19 07:09:36) Associated topics are indexed under C207 - C and C based languages - Pointers and references
Some other Articles
It can take more that one plus one to get two.Private Java Course - A customer's pictures Speed Networking - a great evening and how we arranged it Two by One by Wiltshire Pointers in C As I came back from Tesco Object Oriented Model - a summary of changes from PHP4 to PHP5 Course, right place, right time Gordon Dodge, R.I.P. Helsinki - what comes naturally 1634 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33 at 50 posts per pageThis is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price. Link to Ezine home page (for reading). Link to Blogging home page (to add comments). |
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho | |||||||||||