|
A TUTORIAL ON POINTERS AND ARRAYS IN C - Part 2 |
|
|
|
|
Written by Hemanshu Patel
|
|
Saturday, 29 December 2007 |
|
Page 2 of 6 With that in mind, look at the following program: ------------ program 5.2 --------------------- /* Program 5.2 from PTRTUT10.HTM 6/13/97 */ #include <stdio.h> #include <string.h> struct tag{ /* the structure type */ char lname[20]; /* last name */ char fname[20]; /* first name */ int age; /* age */ float rate; /* e.g. 12.75 per hour */ }; struct tag my_struct; /* define the structure */ void show_name(struct tag *p); /* function prototype */ int main(void) { struct tag *st_ptr; /* a pointer to a structure */ st_ptr = &my_struct; /* point the pointer to my_struct */ strcpy(my_struct.lname,"Jensen"); strcpy(my_struct.fname,"Ted"); printf("\n%s ",my_struct.fname); printf("%s\n",my_struct.lname); my_struct.age = 63; show_name(st_ptr); /* pass the pointer */ return 0; } void show_name(struct tag *p) { printf("\n%s ", p->fname); /* p points to a structure */ printf("%s ", p->lname); printf("%d\n", p->age); } -------------------- end of program 5.2 ----------------
Again, this is a lot of information to absorb at one time. The reader should compile and run the various code snippets and using a debugger monitor things like my_struct and p while single stepping through the main and following the code down into the function to see what is happening.
|
|
Last Updated ( Monday, 06 October 2008 )
|