C Language / Lesson 11 | Back to Index or to Previous Page or to Next Page |
Structures
Download two Sample Programs with structure pointers
/*----- Structures ------*/
/* Following is a structure description */
struct Stud
{ char code[6];
char name[30];
int age;
long fees;
char tel_no[12];
};
/* The previous structure description
does not define any variables this means that
there will be later definitions of variables which have the type of the above
structure,
for this a tag is required that's why we included the tag Stud in the
structure.
Now we can declare variables of the type struct Stud as follows */
struct Stud st[50] , *stp , w ;
/****************************************************
* A structure description with variable declaration *
*****************************************************/
/* ============== with Tag=========== */
struct Stud
{ char code[6];
char name[30];
int age;
long fees;
char tel_no[12];
} w , a[14];
/* ============= w/o Tag =========== */
struct {
char code[6];
char name[30];
int age;
long fees;
char tel_no[12];
} w , a[14];
Structure pointers
Pointers to structures are like all other pointers. They simply point to a structure. For example the definition :
struct Student *sp ;
defines a pointer sp which points to a structure of type struct Student.
Reference to structure members
a member of a structure is denoted as s.member where s is the structure variable.
However, the notation sp.member where sp is a pointer is incorrect because it involve the address of the pointer instead of the address of the structure. To correct this the following notation should be use
(*sp).member
An alternative notation is used for this expression finally because the above notation is not very convenient
sp->member
Back to top or to Previous
Page