Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Retiring, March 2020 - sorry, you have missed our final public course.
The Coronavirus situation has lead us to suspend public training - which was on the cards anyway, with no plans to resume

Please ask about private 'maintenance' training for Python, Tcl, Perl, PHP, Lua, etc
Happily continuing private consultancy / programming work
 
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
C207 - C and C based languages - Pointers and references
  [4560] Variables, Pointers and References - C and C++ - (2015-10-29)
  [4128] Allocating memory dynamically in a static language like C - (2013-06-30)
  [3399] From fish, loaves and apples to money, plastic cards and BACS (Perl references explained) - (2011-08-20)
  [3386] Adding the pieces together to make a complete language - C - (2011-08-11)
  [3242] How to return 2 values from a function (C++ and C) - more uses of pointers - (2011-04-10)
  [3238] Bradshaw, Ben and Bill. And some C and C++ pointers and references too. - (2011-04-09)
  [3121] New year, new C Course - (2011-01-05)
  [3004] Increment operators for counting - Perl, PHP, C and others - (2010-10-18)
  [2670] Pointers to Pointers to Pointers - what is the point? - (2010-03-10)
  [2005] Variables and pointers and references - C and C++ - (2009-01-23)
  [1497] Training Season Starts again! - (2008-01-07)
  [1478] Some new C programming examples - files, structs, unions etc - (2007-12-19)
  [1155] Pointers in C - (2007-04-19)

C210 - C and C based languages - File Handling
  [4340] Simple C structs - building up to full, dynamic example - (2014-12-03)
  [4339] Command line and file handling in C - (2014-12-03)
  [3122] When is a program complete? - (2011-01-06)
  [2571] Reading and writing files in C - (2010-01-12)
  [2002] New C Examples - pointers, realloc, structs and more - (2009-01-20)


Back to
Reading and writing files in C
Previous and next
or
Horse's mouth home
Forward to
C Structs - what, how and why
Some other Articles
What does const mean? C and C++
Sharing variables between files of code in C - extern
Summary of Wiltshire Core Strategy responses
C Structs - what, how and why
The what and why of C pointers
Function Prototypes in C
How to run a successful online poll / petition / survey / consultation
Forums for your Melksham and open source discussions
Extra 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).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2020: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2572_The ... nters.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat