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

What is manipulator in C++?

Answer: Manipulators are special functions that can be included in the I/O statement to alter the format parameters of a stream. Table-2 shows some important manipulator functions that are frequently used. To access these manipulators, the file < iomanip.h > should be included in program.                               Table-2               Manipulators                                                    Equivalent ios function               set...

Explanation of Graphics in C programming

   The header file graphics.h should be included to use graphics related built in functions. Again, the variable gDriver is an integer variable that specifies graphics driver to be used. Device drivers are small programs which talk directly to the hardware. Graphics drivers are a subset of device drivers and are applicable only in the graphics mode. The initialization of this variable to DETECT is to set another variable gMode to the highest resolution available for the detected driver.  The value of this variable gDriver may be another than DETECT (e.g. 1 for CGA, 9 for VGA.). The variable gMode is an integer that specifies initial graphics mode.       The built in function initgraph() is for graphics initialization. The syntax for this is as   initgraph(&graphics_driver, &graphics_mode, path_to_driver);   path_to_driver specifies the directory path where initgraph() looks for graphics drivers ( .BGI files). Thi...

BIOS

BIOS stands for Basic Input Output System. It is is kind of OS pre- attached in the system Design to detect every I/O devices It holds basic drivers -checks all I/O devices -Loads main OS -Responsible for starting System switched on manufactured by AMI,Phonix,award,Baiostar POST error test, boots trap load, BIOS is unintrusive system. CMOS and BIOS are two different components of the motherboard. CMOS work as storage if BIOS. A basic software program containing all BIOS is permanently stored in the ROM. every essencial holds program Tresh passing without knowinig is called intrusive.