Вы находитесь на странице: 1из 19

| |





 

| |




 

| 
  
 
| |




 

 
ƥ Structures
a xccessing structure pointer
a x sample program using structures
ƥ Pointers
| |




 

  
ƥ x structure is a collection of variables
referenced under one name, providing a
convenient means of keeping related
information together.
ƥ x structure declaration forms a template that
may be used to create structure objects (that
is instances of a structure).
ƥ The variables that make up the structure are
called  .
| |




 

  
ƥ Structure members are also referred to as

 .
ƥ xll of the members of a structure are logically
related.
ƥ The key word   tells the compiler that a
structure is being declared.
| |




 

  
ƥ !

O  OO

 
  
 O  
   
 O   

O




| |




 

  
ƥ xt this point no variable is being created and
only the form of the data has been defined.
ƥ When the structure is defined, a compound
variable type is defined but not the variable.
| |




 

  
ƥ One or more structure variables may be created
while declaring a structure.
O  OO

 
  
 O  
   
 O   

O








ƥ This defines a structure type address and declares


variables addr_info, binfoand cinfo of that type.
| |




 

  
ƥ The general form of a structure declaration is,

O  O   


 
  
 
  
 
  
 


O   O
| |




 

"    


ƥ Individual members of a structure are accessed
through the use of the . (dot) operator.

ƥ For !:

OO!"#"

ƥ The general format is,

O  $
   
 
| |




 

"    


ƥ To print the name,

 %%&' 
  (O
O
)%%OO
 
| |




 

"
     
ƥ Refer to Figure 1(Ms Word slide)
| |




 


 
ƥ In general, a variable directly contains a
specific values where as a pointer contains
the address of a variable that contains the
specific value.
ƥ Pointers indirectly references the value.
ƥ Referencing a value through a pointer is
called as    
.
ƥ Pointers must be declared before they can be
used.
| |




 


 
ƥ Example:


* +

ƥ In this example, x is an integer variable and * is the


pointer to the integer variable.
ƥ Each variable declared as a pointer must be preceded
by an asterisk (*).
ƥ Pointers may be initialized to 0, NULL or an address.
ƥ x pointer with 0 or NULL points to nothing.

ƥ Ơ&ơ is called as an address operator which returns


the memory address of its operand.
| |




 


 
ƥ !


 ! ,

+*

* ! -

ƥ In this case, the address of the variable y to pointer


variable x will be stored in x.
| |




 


 
ƥ !
ƥ #
$$$
| |




 


 
ƥ The * operator is referred to as the indirection
operator or de-referencing operator.
ƥ The * operator returns a synonym for the object to
which its pointer operand points. This way of using *
operator is called as %    a pointer.
ƥ De-referenced pointer may also be used to receive an
input value as
cin >> *xptr;
| |




 


 
ƥ |&&   

 %%+* %% 
 ù

 %% %%


 ù

+* ! " assigns the value 7 to x


| |




 


 
ƥ !  refer to Figure 2 and
Figure 3 (Ms Word slide)
| |




 

|  
'   
ƥ !Refer to Figure 4(Ms Word slide)