For 2023 (and 2024 ...) - we are now fully retired from IT training. We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.
Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!
I am also active in many other area and still look after a lot of web sites - you can find an index ((here)) |
The what and why of C pointers
If you put a "*" in front of a variable name as you declare the variable in C, that variable holds the ADDRESS of a value of the type give, rather than the value itself. Thus:
int bill; /* holds an integer */
int *ben; /* holds an ADDRESS. At that address you'll find an integer */
You can then add a "*" in front of a variable name in your executable code to ask for "the contents of", and an "&" to ask for "the address of". What seems like a very simple thing to describe, though ...
a) Leads to a lot of diagrams and explanations on a course
b) Makes a lot of people ask "WHY do we do this?"
Firstly, I've put a sample program showing each of the elements in use, and with lots of comments line by line, [HERE].
Now ... "why" ...
1. If you pass an ADDRESS into a function, you get the ability to alter the value that it points to. If you just copy a value into a function (the default alternative), any changes you make are just going to be to the copy and won't get back to the calling code
2. It makes passing big structures and arrays into functions much more efficient - you just pass a single address rather than duplicating a lot of data
3. You can now pass back multiple parameters from a function
4. You can write common code using a pointer variable name like "current" and have it work on lots of different bits of data at different times, again saving recoding or copying
5. It lets you handle heterogeneous arrays, where the size of each element may in effect vary
6. It allow you to use dynamic memory allocation functions such as calloc and realloc to grab memory as you need it, rather than wastefully coding in such a way that arrays are always big enough to take the largest possible data set your program will ever use.
It's really incredible what a few "*"s and "&"s can do!
(written 2010-01-13)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles C210 - C and C based languages - File Handling [2002] New C Examples - pointers, realloc, structs and more - (2009-01-20) [2571] Reading and writing files in C - (2010-01-12) [3122] When is a program complete? - (2011-01-06) [3386] Adding the pieces together to make a complete language - C - (2011-08-11) [4339] Command line and file handling in C - (2014-12-03) [4340] Simple C structs - building up to full, dynamic example - (2014-12-03) C207 - C and C based languages - Pointers and references [1155] Pointers in C - (2007-04-19) [1478] Some new C programming examples - files, structs, unions etc - (2007-12-19) [1497] Training Season Starts again! - (2008-01-07) [2005] Variables and pointers and references - C and C++ - (2009-01-23) [2670] Pointers to Pointers to Pointers - what is the point? - (2010-03-10) [3004] Increment operators for counting - Perl, PHP, C and others - (2010-10-18) [3121] New year, new C Course - (2011-01-05) [3238] Bradshaw, Ben and Bill. And some C and C++ pointers and references too. - (2011-04-09) [3242] How to return 2 values from a function (C++ and C) - more uses of pointers - (2011-04-10) [3399] From fish, loaves and apples to money, plastic cards and BACS (Perl references explained) - (2011-08-20) [4128] Allocating memory dynamically in a static language like C - (2013-06-30) [4560] Variables, Pointers and References - C and C++ - (2015-10-29)
Some other Articles
What does const mean? C and C++Sharing variables between files of code in C - externSummary of Wiltshire Core Strategy responsesC Structs - what, how and whyThe what and why of C pointersFunction Prototypes in CHow to run a successful online poll / petition / survey / consultationForums for your Melksham and open source discussionsExtra MySQL course dates (2 day course, UK)
|
4759 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, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page
This 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).
|
|