### Pointers And Structures

1. Structure is usually a collection of dissimilar data types; unlike an array which is a collection
of similar data types. Usually a structure is declared first followed by definition of a structure
variable as shown below:

/* declaration of a structure */
struct book
{
char name ;
int numpages ;
float price ;
} ;

/* definition of a structure variable */
struct book b ;

2. A structure variable can be initialized at the same place where it is being defined, as in
struct book b = { "Basic", 425, 135.00 } ;

3. Declaration of a structure and definition of a structure variable can be combined into one. When this
done mentioning the structure name is optional.

struct
{
char name ;
int numpages ;
float price ;
} b = { "Basic", 425, 135.00 } ;

4. Size of a structure variable is sum of sizes of its individual elements. For example, size of b
in (c) above is: 20 + 2 + 4 = 26 bytes.

5. Elements of a structure are stored in adjacent memory locations. For example, the following program would produce the output
4001, 4021, 4023.

6. struct book
{
char name ;
int numpages ;
float price ;
} ;
struct book b = { "Basic", 425, 135.00 } ;
printf ( "%u %u %u", b.name, &b.numpages, &b.price ) ;

7. It is possible to build an array of structures.
struct book
{
char name ;
int numpages ;
float price ;
} ;
struct book b[ ] = {
{ "Basic", 425, 135.00 },
{ "Pascal", 500, 155.00 },
{ "VBasic", 625, 335.00 }
} ;

8. Nested structures are legal as in:

{
char city ;
long int pin ;
} ;
struct emp
{
char n ;
int age ;
float s ;
} ;
struct emp e = { "Rahul", 23, "Nagpur", 440010, 4000.50 } ;

9. Contents of one structure variable can be copied either into another structure variable either piece
meal or at one shot. This is shown below:

struct book
{
char name ;
int numpages ;
float price ;
} ;

struct book b1 = { "Basic", 425, 135.00 } ;
struct book b2, b3 ;

/* piecemeal copying */
strcpy ( b2.n, b1.n ) ;
b2.numpages = b1.numpages ;
b2.price = b1.price ;

/* copying at one shot */
b3 = b2 ;

10. Elements of a structure can be passed to a function.

main( )
{
struct book
{
char name ;
int numpages ;
float price ;
} ;

struct book b1 = { "Basic", 425, 135.00 } ;

/* mixed call - call be value + call by reference */
display ( b1.name, b1.numpages, b1.price ) ;

/* pure call by reference */
show ( b1.name, &b1.numpages, &b1.price ) ;
}

display ( char *n, int nop, float pr )
{
printf ( "%s %d %f", n, nop, pr ) ;
}

display ( char *n, int *nop, float *pr )
{
printf ( "%s %d %f", n, *nop, *pr ) ;
}

11. Entire structure can also be passed to a function.

// declaration must be global.
// Otherwise diaplay1() and show1() can't use it
struct book
{
char name ;
int numpages ;
float price ;
} ;

main( )
{
struct book b1 = { "Basic", 425, 135.00 } ;
// call be value
display1 ( b1 ) ;

// call by reference
show1 ( &b1 ) ;
}

display1 ( struct book b2 )
{
printf ( "%s %d %f", b2.name, b2.numpages, b2.price ) ;
}

show1 ( struct book *b2 )
{
printf ( "%s %d %f", ( *b2 ).name, ( *b2 ).numpages, ( *b2 ).price ) ;
printf ( "%s %d %f", b2->name, b2->numpages, b2->price ) ;
}

12. Self referential structures contain a pointer to itself within its declaration. These are necessary for building linked lists.

struct node
{
int data ;
} ;

What will be the output of the following program

main( )

{

struct s1

{

char *z ;

int i ;

struct s1 *p ;

} ;

static struct s1 a[ ] = {

{ "Nagpur", 1, a + 1 },

{ "Raipur", 2, a + 2 },

{ "Kanpur", 3, a }

} ;

struct s1 *ptr = a ;

printf ( "\n%s %s %s", a.z, ptr->z, a.p->z ) ;

}

Output
Nagpur Nagpur Nagpur

Explanation:
The zeroth and first elements of struct s1 are a char pointer and an int respectively. The second element is what's new.
It is a pointer to a structure. That is, p stores the starting address of a structure variable of the type struct s1. Next, a[ ], an array of such structures is declared as well as initialised. During initialisation the base address of "Nagpur" is stored in a.z, 1 is stored in the element a.i, and a + 1 is assigned to a.p. On similar lines, the remaining two elements of the array are initialised. a.z, a.i and a.p are assigned "Raipur", 2 and a + 2 in that order, and "Kanpur", 3 and a are stored at a.z, a.i and a.p respectively. What exactly do a, a + 1 and a + 2 signify? a, of course, is the base address of the array a[ ]. Let us assume it to be 4000, as shown in Figure 1. Locations 4000 and 4001 are occupied by the char pointer a.z, since a pointer is always two bytes long.

The next two bytes are used to store the integer a.i, and then 4004 and 4005 are used by a.p. Similarly, the next 6 bytes store the first structure a, and the 6 bytes after that contain a, the second structure in the array.

Figure 1.

What will be the output of the following program

main( )

{

struct s1

{

char *str ;

int i ;

struct s1 *ptr ;

} ;

static struct s1 a[ ] = {

{ "Nagpur", 1, a + 1 },

{ "Raipur", 2, a + 2 },

{ "Kanpur", 3, a }

} ;

struct s1 *p = a ;

int j ;

for ( j = 0 ; j <<= 2 ; j++ )

{

printf ( "\n%d " , --a[j].i ) ;

printf ( "%s" , ++a[j].str ) ;

}

}

`Output 0 agpur 1 aipur 2 anpur`

Explanation
The example deals with a structure similar to the one we just encountered. Picking up from the for loop, it is executed for 3 values of j: 0, 1 and 2. The first time through the for loop, j is equal to zero, so the first printf( ) prints --a.i. Since the dot operator has a higher priority, first a.i is evaluated, which is 1. As -- precedes the value to be printed, 1 is first decremented to 0, and then printed out.

The second printf( ) prints the string at address ++a.str. a.str gives the starting address of "Nagpur". On incrementing, it points to the next character, `a' of "Nagpur", so starting from `a', the remaining string "agpur" is outputted. A similar procedure is repeated for j = 1, and then once again for j = 2, following which the execution is terminated.