Pr.Pg Next Pg

Structures as a Member tutorials

Pointer to the structure

  • A pointer can also be associated to a structure.

  • The address of a given structure variable can be obtained by using & operator.

  • There are two methods of defining the pointer to the structure.One by declaring global pointers and other by local declaration

 

Method 1:Global declaration

  • In global declaration the structure variable is defined just after the declaration of the structure and then a pointer variable is declared to the structure.

  • The following example illustrates method 1 as follows

struct date

{

int day;

int month;

int year;

}d1,*p=&d1;

 

  • Here d1 is the structure variable of the type date and p is the pointer variable to the structure variable d1.

  • Here d1 and p are globally declared. Therefore p holds the address of the structure variable d1.

  • In this manner n number of structure variables and their pointers can be declared.

  • However in this method memory space wastage takes place as the all the structure variables may not be used.

 

Method 2: Local declaration

  • In local declaration of the structure variable is defined in a local scope also the pointer variable is declared to that structure variable in that local scope.

  • This method avoids the memory space wastage as the variables are declared and defined in the local scope.

  • The following example illustrates method 2 as follows

 

struct date

{

int day;

int month;

int year;

};

 

void main()

{

date d1;

date *p=&d1;

}

 

Structure as a member

  • Structure variable can be a member of another structure.

  • This is because a structure is a derived data type and any structure can be instantiated anywhere within the scope.

  • The following example illustrates this:

 

 

Example: Program to use a structure as a member

Sam Madam

//WAP to use a structure as a member

#include <stdio.h>

#include <conio.h>

struct time

{

int hours;

int minutes;

int seconds;

};

 

struct timestamp

{

int day;

int month;

int year;

time t1;

};

void main()

{

timestamp ts1;

printf("Enter the day : ");

scanf("%d",&ts1.day);

printf("\nEnter the month : ");

scanf("%d",&ts1.month);

printf("\nEnter the year : ");

scanf("%d",&ts1.year);

printf("\nEnter the hours : ");

scanf("%d",&ts1.t1.hours);

printf("\nEnter the minutes : ");

scanf("%d",&ts1.t1.minutes);

printf("\nEnter the seconds : ");

scanf("%d",&ts1.t1.seconds);

printf("\nTimestamp %d:%d:%d:%d:%d:%d",ts1.day,ts1.month,ts1.year,ts1.t1.hours,ts1.t1.minutes,ts1.t1.seconds);

getch();

}

 

Output

Enter the day : 5

 

Enter the month : 3

 

Enter the year : 1991

 

Enter the hours : 5

 

Enter the minutes : 30

 

Enter the seconds : 65

 

Timestamp 5:3:1991:5:30:65

 

 

Example: Program to enter the real and imaginary parts of two complex numbers and add,subtract and multiply them

Sam Madam

/*WAP to enter the real and imaginary parts of two complex numbers and add,subtract and multiply them

*/

#include<stdio.h>

#include <conio.h>

 

struct complex

{

float real, imag;

};

 

void read(complex &a)

{

printf("Enter real part: ");

scanf("%f",&a.real);

printf("Enter imaginary part: ");

scanf("%f",&a.imag);

}

 

complex read()

{

complex a;

printf("Enter real part: ");

scanf("%f",&a.real);

printf("Enter imaginary part: ");

scanf("%f",&a.imag);

return a;

}

 

complex add(complex a, complex b)

{

complex c;

c.real = a.real + b.real;

c.imag = a.imag + b.imag;

return c;

}

void sub(complex a, complex b, complex &c)

{

c.real = a.real - b.real;

c.imag = a.imag - b.imag;

}

 

void mult(complex a, complex b, float &c, float&d)

{

c = a.real * b.real - a.imag * b.imag;

d = a.imag * b.real + a.real * b.imag;

}

 

void print(complex x)

{

if(x.imag >= 0)

printf("%.0f + %.0fi\n",x.real,x.imag);

else

printf("%.0f %.0fi\n",x.real,x.imag);

}

 

void main()

{

complex x, y, z;

read(x);

y = read();

z = add(x, y);

printf("The addition is: ");

print(z);

sub(x, y, z);

printf("The subtraction is: ");

print(z);

mult(x, y, z.real, z.imag);

printf("The multiplication is: ");

print(z);

getch();

}

 

Output

Enter real part: 3

Enter imaginary part: 7

Enter real part: 9

Enter imaginary part: 8

The addition is: 12 + 15i

The subtraction is: -6 -1i

The multiplication is: -29 + 87i

 

Structure inside a structure

  • A structure can be defined inside a structure definition. This method is also known as nested structure.The structure members can be accessed in the same way as that of the other structure variables are to accessed.

  • The following examples demonstrates the nested structures

 

struct student

{

Introllno;

char name[20];

struct address

{

char add1[30];

char add2[30];

long pin;

}a1;

}s1={101,”sam”,”Mumbai”,”Maharashtra”,400064};


Pr.Pg border                                              Next Pg