Escolar Documentos
Profissional Documentos
Cultura Documentos
struct date
{
int day;
int month;
int year;
char month_name[10];
};
Note:
The keyword struct and the use of curly brackets (braces) to delimit the
declaration. Most importantly, all we have said so far is what the shape" of
date is; we have not created any variables.
struct date d;
Declaring Structures
(cont’d)
• What this gives us is a variable called d, an information record with
(in this case) four members (day, month, year, month_name). Each
member can be of a different type and hence of a different length.
• The generic structure has a name (date). The specic variable has a
name (d).
d.month_name[3]
Simple Program
#include <strings.h> /* We need the system-provided strcpy() */
int main()
{
struct date /* declare the shape of date */
{
int day;
int year;
char month_name[10];
};
struct date d; /* create a variable d of type date */
d.day= 25;
strcpy(d.month_name,"January");
d.year= 1991; /* suppose positive is A.D. */
struct person
{
char person_name[20];
struct date birth_date;
struct date death_date;
}
• To access the various members we simply extend the dot notation. For example,
given
struct person p;
we can refer to that person's birthdate as
p.birth_date
and the year of that person's birth is:
p.birth_date.year
while the first letter of the month of birth is:
p.birth_date.month_name[0]
structs combined to make
more elaborate structures
• Similarly you can have arrays of structs:
Q5_class[27].name etc.
struct complex
{
double real_part;
double imag_part;
} x, y, z[10];
ptr->eno=1;
strcpy((*ptr).ename,”Asad”);
printf( “ Emp No. :%d “, ptr->eno);
printf(“ Emp Name: “);
puts(ptr->ename);
Union
• union
– Memory that contains a variety of objects over time
– Only contains one data member at a time
– Members of a union share space
– Conserves storage
– Only the last data member defined can be accessed
• union definitions
– Same as struct
union Number {
int x;
float y;
};
union Number value;
Union (con’td)
• Valid union operations
– Assignment to union of same type: =
– Taking address: &
– Accessing union members: .
– Accessing members using pointers: ->
Bitwise Operators
• All data represented internally as sequences of bits
– Each bit can be either 0 or 1
– Sequence of 8 bits forms a byte
Operator Description
& bitwise AND The bits in the result are set to 1 if the corresponding bits in the
two operands are both 1.
| bitwise inclusive The bits in the result are set to 1 if at least one of the
OR corresponding bits in the two operands is 1.
^ bitwise exclusive The bits in the result are set to 1 if exactly one of the
OR corresponding bits in the two operands is 1.
<< left shift Shifts the bits of the first operand left by the number of bits
specified by the second operand; fill from the right with 0 bits.
>> right shift Shifts the bits of the first operand right by the number of bits
specified by the second operand; the method of filling from the
left is machine dependent.
~ one’s complement All 0 bits are set to 1 and all 1 bits are set to 0.
Fig. 10.6 The bitwise operators.
Bitwise Operators (con’td)
Bit 1 Bit 2 Bit 1 | Bit 2
0 0 0
1 0 1
0 1 1
1 1 1
Fig. 10.11 Results of combining two bits with the bitwise inclusive OR
operator |.
So:
then:
or 0 (decimal)
or 8 (decimal)
NOTE:
Shifting is much faster than actual multiplication (*) or division (/) by 2. So if you
want fast multiplications or division by 2 use shifts.
Exercise
• Write any program that pass arrays of structure to the
function.