Академический Документы
Профессиональный Документы
Культура Документы
struct StudentRecord {
char *name; // student name
double hw[3]; // homework grades
double test[2];// test grades
double ave; // final average
};
Structure declarations
struct S {int a, b; char *p;}; /* omit variables
*/
No variables are declared, but there is now a type
struct S that can be referred to later
struct date
struct tag
{
{
int day;
type variable-name;
int month
type variable-name;
int year;
type variable-name;
};
}structure variables;
E-1
/* Example program for using a structure*/
#include< stdio.h > printf(“Enter the age of the student”);
void main() scanf(“%d”,&new student.age);
{ printf(“Student information\n”);
int id_no; printf(“student id_number=
char name[20]; %d\n”,newstudent.id_no);
char address[20]; printf(“student name=
%s\n”,newstudent.name);
char combination[3];
printf(“student Address=
int age; %s\n”,newstudent.address);
}newstudent;
printf(“students combination=
printf(“Enter the student information”); %s\n”,newstudent.combination);
printf(“Now Enter the student id_no”); printf(“Age of student=
scanf(“%d”,&newstudent.id_no); %d\n”,newstudent.age);
printf(“Enter the name of the student”); }
scanf(“%s”,&new student.name);
printf(“Enter the address of the student”);
scanf(“%s”,&new student.address);
printf(“Enter the cmbination of the student”);
scanf(“%d”,&new student.combination);
The ‘typedef ‘ Keyword.
as in
struct date
typedef struct date
{ int day ; { int day ;
int month;
int month;
}; };
Printf(“%s”,stu.name);
stu.hw[2] = 82.3;
stu.ave = total/100;
Refrencing Structure elements.
D1.year=1988;
D2.day= 2;
.
struct_name element_name
da month year
y
D1 D1.year
D2
D2.day
Structure Assignments.
One structure variable can be assigned another
variable of the same structure type.
even if the data members are same (in no. and in type
) we cannot assign to different structure type.
Only Assignment (=) is possible for two similar
structures.
Other Operations like ( == ) OR ( ! = ) are not
possible in most versions of compliers.
Structure Assignment
StudentRecord s1,s2;
s1.name = "Joe Student";
…
s2 = s1; Copies the entire structure
Structure Assignments.
Eg.
p2=p1; n2=n1;
p2=n2;
//error: type
mismatch//
Pointers to Structures
Pointers to structures are used often.
There is another member access t e r"!
po in
operator used with pointers: -> a "
ike s l
loo k
it
StudentRecord *sptr;
…
cout << "Name is" << sptr->name;
cout << "Ave is " << sptr->ave;
Structures and Arrays
Array of structures.
eg. Struct emp e[10];
Arrays within structure
eg. Struct student
{
char name[20];
int marks[5];
};
E-2 Void main()
{
int I,n;
printf(“Enter the number of students”);
scanf(“%d”,&n);
#include< stdio.h > printf(“ Enter Id_no,name address combination
{ age\m”);
struct info for(I=0;I < n;I++)
scanf(%d%s%s%s
{ %d”,&std[I].id_no,std[I].name,std[I].address,
int id_no; char name[20]; std[I].combination,&std[I].age);
printf(“\n Student information”);
char address[20]; for (I=0;I< n;I++)
char combination[3]; printf(“%d%s%s%s%d\n”,
int age; ”,std[I].id_no,std[I].name,std[I].address,std[I].co
mbinatio
} n,std[I].age);
struct info std[100]; }
E-3 Comparison example
#include<stdio.h> void main()
{
#include<conio.h> clrscr();
struct student s1.age=25;
s1.avg=9.5;
{ s2.age=30;
int age; s2.avg=8.5;
float avg; s3=s2;
if(s2.age==s3.age && s2.avg==s1.avg)
}s1,s2,s3; printf("Both S2 and S3 are Same");
else
printf("Both are different");
getch();
}
Nested Structures
Struct addr
structure within a structure
{ int houseno;
inner structure should be defined
before outer structure . char locality[20];
Elements are refrenced outermost to char city[20];
innermost.
};
here shrey.address.houseno=3; Struct employee
{int name;
addr address;
};
Struct employee shrey;
Structures and functions.
Correct Ans is
D) NO error !
struct date {
short day;
short month;
short year ;
}bdate,joindate;
is also correct
Unions
Like structures, but every member occupies the same region of
memory!
Structures: members are “and”ed together: “name and
species and owner”
Unions: members are “xor”ed together
union VALUE {
float f;
int i;
char *s;
};
/* either a float xor an int xor a string */
Unions
Storage
size of union is the size of its largest member
avoid unions with widely varying member sizes;
for the larger data types, consider using pointers instead
Initialization
Union may only be initialized to a value appropriate for the
type of its first member