Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

8.

3
(accessing structure members)

(struct_var) . (period)
(element_name) member_name

struct_var. member_name
person.emp_name
person.position
person.salary
x.a, x.b, x.c a, b c
( 1 2 )

8.1
1

/*
struct1.c
#include<stdio.h>
#include<conio.h>
void main(void)
{
struct record
{
int i;
char j;
float k;
};
struct record one,two;

*/
/* 1 */
/* 2 */
/* 3 */
/* 4 */
/* 5 */
/* 6 */
/* 7 */
/* 8 */
/* 9 */
/* 10 */
/* 11 */

clrscr();
/* 12 */
one.i=7; one.j='I'; one.k=1.2345;
/* 13 */
two.i=4; two.j='J'; two.k=9.8765;
/* 14 */
printf("I = %d, J = %c, K = %.5f\n",one.i,one.j,one.k); /* 15 */
printf("I2 = %d, J2 = %c, K2 = %.5f\n",two.i,two.j,two.k);/* 16 */
printf("\nPress any key back to program...");
/* 17 */
getch();
/* 18 */
}
/* 19 */

8.1
5 record
11 one two
13 14
15 i, j, k one
16 i, j, k two
8.2
2
/*
struct2.c
#include<stdio.h>
#include<conio.h>
void main(void)

*/
/* 1 */
/* 2 */
/* 3 */

/* 4 */
struct simple
/* 5 */
{
/* 6 */
int num;
/* 7 */
char ch;
/* 8 */
} a,b;
/* 9 */
clrscr();
/* 10 */
a.num = 9;
a.ch='K';
/* 11 */
b.num = a.num+6;
b.ch=a.ch - 1;
/* 12 */
printf("Number a = %d, Char a = %c\n", a.num, ++a.ch);
/* 13 */
printf("Number b = %d, Char b = %c\n", b.num-6, b.ch);
/* 14 */
printf("\nPress any key back to program...");
/* 15 */
getch();
/* 16 */
}
/* 17 */

8.2
5 simple
11 12
13 num, ch a
14 num, ch b

You might also like