Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
For 2023 - 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))
Structures v Structure Pointers in C. How, which, why.

A Structure in C is a series of sequential memory locations in which you may store and access all the various attributes of a piece of composite data; you define what each of the member elements is with a typedef and then declare variables of that new type. There's a simple example [here] which defines a data type "health" with two float members - h and w for height and weight:

  typedef struct {
    float h;
    float w;
    char name[21]; } health;

You can then declare as many variables as you like of this data type, for example:
  health primeminister;
and reference the members using a dot notation, for example:
  primeminister.w = 80;
  strncpy(primeminister.name,"David C",20)

and you can pass the data (using the single name) into functions too.

But when you pass a struct variable in to a function, you may be copying quite a bit of data and you are certainly copying the data. Which is less than efficient, and means that you're not able to return any changes within the structure - altering a copy does not mean that you alter the original, which remains unchanged in the calling code.

This issue of efficiency and flexibility is usually solved by using (and passing) pointers to structures rather than structures themselves. The variable will be declared as a pointer, and you'll need to take the additional step of ensuring that there's memory allocated for it too:
  health *primeminister;
  primeminister = (health *)calloc(1,sizeof(health));

You can then reference it through the "." notation using pointers:
  (*primeminister).h = 1.78;
But that gets a bit messy, so there's a shorthand ( ->) which you can use instead:
  primeminister->w = 85;

Full source example, using a struct pointer, is [here] to compare to the previous one that just used a straight struct.

You are strongly advised to use struct pointers most of the time; they're efficiently passed around, provide an easy route to modifying your data, and allow you far more control over the persistence of the object, as you're at liberty to allocate it on the heap and retain it until it's no longer needed, rather than using a default scoping as supplied to local variables on the stac, or global ones via extern.

Example ... from yesterday's Programming in C course.

Other examples:
basic structure setup and use - a first example
Struct and struct pointers in same program
reading a file into an array os structs
(written 2011-01-25)

Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
C209 - C and C based languages - Structures and Unions
  [1478] Some new C programming examples - files, structs, unions etc - (2007-12-19)
  [1572] C - structs and unions, C++ classes and polymorphism - (2008-03-13)
  [1584] Using Structs and Unions together effectively in C - (2008-03-21)
  [1669] What are Unions (C programming) - (2008-06-08)
  [2573] C Structs - what, how and why - (2010-01-13)
  [3122] When is a program complete? - (2011-01-06)
  [3386] Adding the pieces together to make a complete language - C - (2011-08-11)

Back to
Setting up arrays in C - fixed size at compile time, or dynamic
Previous and next
Horse's mouth home
Forward to
Strings in C
Some other Articles
Looking back at www.wellho.net
OpenGL / C / C++ - an example to get you started
Hotel star ratings - towards a better system of review
Strings in C
Structures v Structure Pointers in C. How, which, why.
Setting up arrays in C - fixed size at compile time, or dynamic
On time
Private and Public - and things between
Wiltshire Rail services - a golden opportunity
Django - separating the HTML from the view / model
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., 2023: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3145_Str ... -why-.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb