c Programming-chapter 10
c Programming-chapter 10
PRESENTED BY
SHEYONA G
CHAPTER-10
STRUCTURE & UNION
What is Structure
struct structure-name
{
type member1;
type member2;
.
.
.
type member n;
}
DECLARING STRUCTURE VARIABLES
After defining a structure format we can declare variable of that structure type. It is same as like any other data type
declaration
Syntax:
Where,
struct is a keyword
structure-name is the name of the structure
structure-variable-name is the variable of the type structure
A member of a structure can be accessed by specifying the variable name followed by a period operator
or dot operator or member operator which in turn followed by the member name.
Syntax:
struct student
{
int tkno,total;
} s;
void main()
{
clrscr();
printf(“Enter tkno, total\n”);
scanf(“%d”,&s.tkno);
scanf(“%d”,&s.total);
printf(“Tkno=%d\n”,s.tkno);
printf(“Total=%d\n”,s.total);
printf(“Size=%d\n”,sizeof(s));
printf(“Size=%d”,sizeof(struct student));
}
Example 2
Structure initialization in C
Structure Operations
1. Copying of structure variables
2. Comparison of two structure variables or members
3. Arithmetic operations on structures
Copying of structure variables
By using assignment operator(=) user can copy two structure variables of same type
Ex: Ex:
struct abc struct abcd
{ {
char name[20]; char name[20];
int salary,id; int salary,id;
}a,b; }c,d;
Comparison of two structure variables or members
Copying of two structure variable is allowed but comparison of two structure variables is not allowed.
For example,
a==b
a!=b
is not allowed.
Arithmetic operations on structures
Like other variables, any other operation that can be performed on members of structure variables.
For ex:
Ex:
struct student
{
int tkno;
char name[20];
};
Ex:
{
struct student scanf(“%d”,&s[i].tkno);
{ scanf(“%s”,s[i].name);
int tkno; }
char name[20]; for(i=0;i<n;i++)
}; {
printf(“Details of %d student\n”,i+1);
void main() printf(“Tkno=%d\n”,s[i].tkno);
{ printf(“Name=%s\n”,s[i].name);
srtuct student s[10]; }
int i,n; }
printf(“How many details you want to store\n”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
Arrays within structures
Inside the structure user can use one dimensional or two-dimensional arrays is called arrays within
structures.
Ex:
struct student
{
int number;
float mark[3];
}s[5];
Here, the member mark contains three elements, mark[0], mark[1], mark[2].
Ex: s[0]. mark[1];
Ex:
struct student {
{ s[i].total= s[i].total+s[i].mark[j];
int number, total;
int mark[3]; printf(“Mark[%d]=%d\n”,i+1,mark[j]);
}; }
struct st_name1
{
type member1;
type member2;
struct st_name2
{
type member1;
type member2;
.
type member n;
}st_var_name;
type member n;
};
Where
Which of the following cannot be a structure member? integer character another function
structure
For accessing structure member the period operator is followed by member name structure name tag name file name
the _______
To copy one structure variable to another structure variable
_____operator is used. assignment (=) strcpy() %% ()
When you are doing partial initialization in structure the uninitialized Zero NULL void 1
float member will take the value ____
When you are doing partial initialization in structure the uninitialized Zero void 1 NULL
integer member will take the value ____
struct student {int tno;char name[10];}s1; the size of s1 is ______ 12 10 2 4
bytes
struct student {int tno;char name[10];}s1={23}; The value of the NULL('\0') 0 23 10
member name is ______
struct book{ cahr bname[20],title[20]; float price;}b1;The size of b1 is 44 20 4 40
_____bytes
A structure within a structure is called ______ nesting of union nested array recursion
structure
Which of the following method is most powerful way of tagged structure type-
user defined
defining the structure? structure variable defined
member address
The dot operator is also called as ___________ arithmetic relational
operator operator