Quick Navigation Bar strings :: structures :: advanced data structures [ toc | forums ] |
Note: If the document URL does not begin with https://randu.org/tutorials/c/ then you are viewing a copy. Please direct your browser to the correct location for the most recent version. |
struct student { char *first; char *last; char SSN[9]; float gpa; char **classes; }; struct student student_a, student_b;Another way to declare the same thing is:
struct { char *first; char *last; char SSN[10]; float gpa; char **classes; } student_a, student_b;As you can see, the tag immediately after struct is optional. But in the second case, if you wanted to declare another struct later, you couldn't.
struct student_t { char *first; char *last; char SSN[10]; float gpa; char **classes; } student, *pstudent;Now we have created a student_t student and a student_t pointer. The pointer allows us greater flexibility (e.g. Create lists of students).
.
(period). For example, to assign the SSN of
student_a
:
strcpy(student_a.SSN, "111223333\0");
struct student *student_a;But how do we dereference the pointer to the struct and its fields? You can do it in one of two ways, the first way is:
printf("%s\n", (*student_a).SSN);This would get the SSN in student_a. Messy and the readability is horrible! Is there a better way? Of course, programmers are lazy! :)
->
.
The above example using the new operator:
printf("%s\n", student_a->SSN);
*student_a
could we start assigning things to pointer fields inside the structure?
No. You must malloc space for each individual pointer within
the structure that is being pointed to.typedef struct { char *first; char *last; char SSN[9]; float gpa; char **classes; } student; student student_a;Now we get rid of those silly
struct
tags. You can
use typedef
for non-structs:
typedef long int *pint32; pint32 x, y, z;x, y and z are all pointers to long ints.
typedef
is
your friend. Use it.struct conditions { float temp; union feels_like { float wind_chill; float heat_index; } } today;As you know, wind_chill is only calculated when it is "cold" and heat_index when it is "hot". There is no need for both. So when you specify the temp in today, feels_like only has one value, either a float for wind_chill or a float for heat_index.
enum e_months {JAN=1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}; typedef enum e_months month; month currentmonth; currentmonth = JUN; /* same as currentmonth = 6; */ printf("%d\n", currentmonth);We are enumerating the months in a year into a type called month. You aren't creating a type, because enumerated types are simply integers. Thus the printf statement uses
%d
,
not %s
.JAN=1
tells C to
make the enumeration start at 1 instead of 0.#define JAN 1 #define FEB 2 #define MAR 3 /* ... etc ... */
&
operator may be used with structs to show
addresses.->
for dereferencing the pointer.typedef
can help you clear your code up and can
help save some keystrokes.#define
statements.
Notice: Please do not replicate or copy these pages and
host them elsewhere. This is to ensure that the latest version can always
be found here.
Disclaimer: The document author has published these pages
with the hope that it may be useful to others. However, the document
author does not guarantee that all information contained on these
webpages are correct or accurate. There is no warranty, expressed or
implied, of merchantability or fitness for any purpose. The author does
not assume any liability or responsibility for the use of the information
contained on these webpages.
If you see an error, please send an email to the address below indicating
the error. Your feedback is greatly appreciated and will help to
continually improve these pages.