Pointers and structures:
We know the name of an array stands for the address of its zeroth element the same concept applies for names of arrays of structures. Suppose item is an array variable of struct type. Consider the following declaration:
struct products
{
char name[30];
int manufac;
float net;
item[2],*ptr;
struct products
{
char name[30];
int manufac;
float net;
item[2],*ptr;
this statement declares item as array of two elements, each type struct products and ptr as a pointer data objects of type struct products, the
assignment ptr=item;
would assign the address of zeroth element to product[0]. Its members can be accessed by using the following notation.
ptr- >name;
ptr- >manufac;
ptr- >net;
ptr- >manufac;
ptr- >net;
The symbol - > is called arrow pointer and is made up of minus sign and greater than sign. Note that ptr- > is simple another way of writing product[0].
When the pointer is incremented by one it is made to pint to next record ie item[1]. The following statement will print the values of members of all the elements of the product array.
for(ptr=item; ptr< item+2;ptr++)
printf(“%s%d%f\n”,ptr- >name,ptr- >manufac,ptr- >net);
printf(“%s%d%f\n”,ptr- >name,ptr- >manufac,ptr- >net);
We could also use the notation
(*ptr).number
to access the member number. The parenthesis around ptr are necessary because the member operator ‘.’ Has a higher precedence than the operator *.
No comments:
Post a Comment