Documente Academic
Documente Profesional
Documente Cultură
Basics of structures
Typedef
Unions
Bitwise operators
Bit fields
Enumeration constants
Data Hierarchy
Byte
8 bits (ASCII character A = 01000001)
Field
Group of characters (character string Fred)
Record
Composed of related fields (struct)
File
Group of related records (student record file)
Database
Group of related files (students, faculty, and staff files)
Structure Declaration
struct student{
char first[20];
Collection of related
char
*last;
variables
int age;
Structure tag
double gpa;
student
};
Structure
Structures members
first, last, age, gpa, s
Structure Definition
Define & initialize
with list of variables
struct student s1 =
{"Ted","Tanaka", 22,
2.22};
Accessing Members
Structure member
operator, or dot
operator
For direct variables
(.)
Structure pointer
operator
For pointers (->)
printf("%s %d\n",
s1.last, s1.age);
/*Tanaka 22*/
printf("%s %d\n",
(*sPtr).last, sPtr->age);
/*Suzuki 33*/
sizeof Operator
Returns the size in bytes of a data type
char a, b[10];
int c, d[10];
double e, f[10];
printf("sizeof(a) = %d\n",sizeof(a));
printf("sizeof(b) = %d\n",sizeof(b));
. . .
/* sizeof(a) = 1
/* sizeof(c) = 4
/* sizeof(e) = 8
sizeof(b) = 10 */
sizeof(d) = 40 */
sizeof(f) = 80 */
sizeof Operator
Examples with pointers
char b[10], *p1 = b;
int d[10], *p2 = d;
double f[10], *p3 = f;
printf("sizeof(p1) = %d ",sizeof(p1));
printf("sizeof(*p1) = %d\n",sizeof(*p1));
. . .
/* sizeof(p1) = 4
/* sizeof(p2) = 4
/* sizeof(p3) = 4
sizeof(*p1) = 1 */
sizeof(*p2) = 4 */
sizeof(*p3) = 8 */
sizeof Operator
Examples with structures
struct student s1={"Ted","Tanaka", 22,2.22};
struct student s2;
struct student *s3 = &s2;
struct student s4[10];
printf("sizeof(s1) = %d\n", sizeof(s1));
. . .
/* sizeof(s1)
sizeof(s2)
sizeof(s3)
sizeof(s4)
=
=
=
=
40
40
4
400 */
sizeof Operator
struct student{
char first[20]; /* 20 bytes */
char *last;
/* 4 bytes */
int age;
/* 4 bytes */
double gpa;
/* 8 bytes */
};
/* total =
36 bytes ?? */
Arrays of Structures
Can also have an array of a structure
main(){
struct student s[100]={"","", 0, 0.0};
printStudent(s[0]);
}
/* Name =
age = 0
gpa = 0.00 */
struct A{
/*See exercise1.txt*/
char b;
int c;
};
struct A fun(struct A);
main(){
struct A d = {'e', 71}; /* sizeof(d)=8 */
struct A e[20];
printf("sizeof(e)=%d\n", sizeof(e));
e[0] = fun(d);
printf("b=%c c=%d\n", d.b, d.c);
printf("b=%c c=%d\n", e->b, (*e).c);
}
struct A fun(struct A a){
printf("b=%c c=%d\n", a.b++, ++a.c);
return a;}
typedef
Creates new data
type name
Integer used as
a synonym for int
IntegerPtr used as
synonym for int *
main(){
Integer a = 10;
IntegerPtr b = &a;
a++;
printf("a = %d\n",a);
printf("b = %d\n",*b);
}
/*a = 11
(See program at typedef.txt)
b = 11*/
typedef
Creates synonyms
for previously
declared data types
. . .
typedef struct
student Student;
typedef
Does not create a
new type
Instead adds a new
name for an exiting
type
Similar to #define,
except it is
interpreted by the
compiler
Example Program
See fig10_03.c from D & D textbook
Represents a deck of cards as an array of
structures
To shuffle the cards, the program randomly
swaps the cards
Output displays 52 cards in two columns
Note the use of the dot operator (.) and the use
of typedef
typedef int A;
/*See exercise2.txt*/
typedef char B;
struct C {
A a;
B b;
};
typedef struct C D;
typedef D *E;
main(){
D d = {40, 'd'};
E e = (E) malloc(sizeof(D));
printf("e = %d %c \n",e->a, e->b);
e->a = d.a;
e->b = 'z';
d.a++;
printf("d = %d %c \n",d.a, d.b);
printf("e = %d %c \n",e->a, e->b);}
Unions
Similar to struct
Contains multiple
data types
Members share the
same data space
Can manipulate
different kinds of
data in a single unit
of storage
union number{
int x;
/*4 bytes*/
double y;
/*8 bytes*/
char z[20];
/*20 bytes*/
};
Unions
union number num;
num.x = 65;
printf("%d\n %f\n
%s\n\n", num.x, num.y,
num.z);
num.y = 1.000001;
printf("%d\n %f\n
%s\n\n", num.x, num.y,
num.z);
strcpy(num.z,"1234567890
123456789");
printf("%d\n %f\n
%s\n\n", num.x, num.y,
num.z);
65
213568. . .2.000000
A
208632331
1.000001
zo
875770417
0.000000
1234567890123456789
/*See exercise3.txt*/
union unionX{
int i;
char c[4];
};
main(){
union unionX x;
x.i = 0x41;
printf("%x %x %x %x %x\n", x.i, x.c[0],
x.c[1], x.c[2], x.c[3]);
x.c[0]='a';
x.c[1]='b';
x.c[2]='c';
x.c[3]='d';
printf("%x %x %x %x %x\n", x.i, x.c[0],
x.c[1], x.c[2], x.c[3]);
}
Bitwise Operators
Instructions are applied to each bit
A & B bitwise AND
Set to 1 if both bits are 1
A | B bitwise OR
Set to 1 if at least one bit is 1
A ^ B bitwise exclusive OR
Set to one if only 1 bit is set to 1
~ A ones complement
All 1s become 0s & all 0s become 1s
Bitwise Operators
A << B left shift
Shift the bits in A to the left B times
Fill in 0s from the right
Same as multiplying by a power of 2
Assignment Operators
x = x + 5;
Can also be written as x += 5;
a *= b + 5;
Evaluates to a = a *(b + 5);
And not
a = a * b + 5;
Displaying Bits
See fig10_07.c in D & D textbook (slightly
altered)
Prints the binary representation of 32-bit
unsigned integers
Uses the AND operator and a operand called a
mask
Bit Fields
Used to save space
Stores data by byte
Must use type
unsigned int
struct bitNum{
unsigned a:1;
/*1 bit*/
unsigned b:2;
/*2 bits*/
unsigned c:3;
/*3 bits*/
unsigned d:4;
/*4 bits*/
};
/*Bit-field Example*/
struct bitNum{
unsigned a:1;
unsigned b:2;
unsigned c:3;
unsigned d:4;
};
main(){
struct bitNum x;
x.a = x.b = x.c = x.d = 15;
printf("%u %u %u %u\n",x.a,x.b,x.c,x.d);
}
/*1 3 7 15*/
(See program at bitFields.txt)
/*See exercise4.txt*/
struct bitNum{
unsigned a:1;
unsigned b:2;
unsigned c:3;
unsigned d:4;
};
main(){
struct bitNum x;
x.a = x.b = x.c = x.d = 0xA;
printf("%d %d %d %d\n",x.a,x.b,x.c,x.d);
}
Enumeration Constants
Set of integer constants
represented by
identifiers
Values start with 0
(unless otherwise
specified) and
increment by 1
See fig10_18.c in D &
D textbook
enum months
= 1, FEB,
APR, MAY,
JUL, AUG,
OCT, NOV,
{ JAN
MAR,
JUN,
SEP,
DEC };
Enumeration Constants
enum months m;
const char *name[] =
{"error", "January",
"February", "March",
"April", "May", "June",
"July", "August",
"September", "October",
"November", "December"};
for(m=JAN;m<=DEC;m++)
printf("%9s\n",name[m]);
January
February
March
April
May
June
July
August
September
October
November
December