Pointers And Structures


Pointers And Structures


Pointers And Structures
We know that the name of an array stands for the address of its zeroth element.The samething is true of the names of arrays of structure variables.Suppose product is an array variable of structtype.The name product represents the address of its zeroth element consider the following
declaration.

  struct inventory
  {
   char name[30];
   int number;
   float price;
  } product[2],*ptr;
  This statement declares product s an array of two elements,each of the type struct inventory and ptr as a pointer to data objects of the type struct inventory.The assignment ptr=product;
would assign the address of the zeroth element of product to ptr.This is,the pointer ptr will now point to product[0].Its members can be accessed using the following
notation.
        ptr --> name
ptr--> number
ptr --> price
  The symbol -->is called the arrow operator and is made up of a minus sign and a greater than sign.Note that ptr--> is simply another way f wrting product[0].When the pointer
ptr is incremented by one,it is made to point to the next  record. i.e,product[1].We could use the notation  (*ptr).number   to access the member number.The parantheses around *ptr
are necessary because the member operator "." has a higher precedence than the operator *

  A program to illustrate the use of a structure pointer to manipulate the elements of an array of structures the program highlights all the features discussed above.Note that the pointer ptr(of type struct invert) is also used as the loop control index in for loops

    

No comments:

Post a Comment