Skip to main content

Structure and Pointer


   To store address of a structure type variable, we can define a structure type pointer variable as normal way. Let us consider an structure book that has members name, page and price. It can be declared as
           struct book
                 {
                    char name[10];
                   int page;
                   float price;
                    };
Then, we can define structure variable and pointer variable of structure type. This can be done as
         struct book b, *bptr;
       Here,
                     b is the structure variable and bptr is pointer variable which points address of structure variable. It can be done as
                    bptr=&b;
        Here, the base address of b can assigned to bptr pointer.

An individual structure member can be accessed in terms of its corresponding pointer variable by writing
            ptr_variable->member
  Here, -> is called arrow operator and there must be pointer to the structure on the left side of  this operator.

Example:
     Create a structure named book which has name, pages and price as member variables. Read name of book, its page number and price. Finally display these members’ value. Use pointer to structure instead of structure itself to access member variables.

  void main()
{
    struct book
         {
         char name[20];
         int pages;
         float price;
          };
  struct book b={"Let us C",230,456.50},*ptr; // structure initialization
  ptr=&b;
  clrscr();
  printf("\nName=%s\tPages=%d\tPrice=%f",ptr->name,ptr->pages,ptr->price);
                       /* same as b.name, b.pages and b.price */
  getch();
}


Here, ptr->name refers the content at address pointed by ptr variable.

Comments

Popular posts from this blog

Object Oriented Programming

Object Oriented Programming Programming paradigm that represents the concept of "objects" that have data fields (attributes that describe the object) and associated procedures known as methods Programming methodology based on objects, instead of just functions and procedures Focuses on data rather than process As individual objects can be modified without affecting other aspects of the program, it is easier for programmers to structure and organize software programs Easier to update and change programs written in object-oriented languages Simula was the first object oriented programming language Eg: C++, Java, etc. Features of OOPS Objects Referred as instance of class Basic run-time entities in an object-oriented system a person, a place, a bank account, a table of data, etc can be an object They occupy space in memory that keeps its state  Each object contains data and code to manipulate the data  Classes Blue print or prototype  which defi

How structure elements are stored?

  The elements of a structure are always stored in contiguous memory locations. This can be illustrated as void main() {     struct student      {      int roll;      float marks;      char remarks;   };   struct student st={200,60.5,'P'};   printf("\nAddress of roll=%u",&st.roll);   printf("\nAddress of marks=%u",&st.marks);   printf("\nAddress of remarks=%u",&st.remarks);   getch(); } Output: Address of roll=65518 Address of marks=65520 Address of remarks=65524                  st.roll               st.marks                          st.remarks 200 60.5 ‘P’               65518             65520                               65524           

How containership is different than inheritance ?

               Inheritance is the mechanism of deriving properties of one class into another. While containership is mechanism in which one class contain objects of other classes as its member.                     class alpha{_ _ _ _ _ };                     class beta{_ _ _ _ _ };                      class gamma                      {                         alpha a;                         beta b;                       _ _ _ _ _ };            All objects of gamma class will contain the objects a and b, this kind of relationship is called containership or nesting.