lecture on c programming structure and union
TRANSCRIPT
-
8/10/2019 Lecture on C Programming Structure and union
1/33
C Structures, Unions, Bit
Manipulations and Enumerations Basics of structures
Typedef
Unions
Bitwise operators
Bit fields Enumeration constants
-
8/10/2019 Lecture on C Programming Structure and union
2/33
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)
-
8/10/2019 Lecture on C Programming Structure and union
3/33
Structure Declaration
Structure
Collection of related
variables
Structure tag
student
Structures members
first, last, age, gpa, s
struct student{
char first[20];
char *last;int age;
double gpa;
};
-
8/10/2019 Lecture on C Programming Structure and union
4/33
Structure Definition
Define & initialize
with list of variables
Define & initialize
using the dot
operator (structure
member operator)
struct student s1 =
{"Ted","Tanaka", 22,
2.22};
struct student s2;
strcpy(s.first, "Sally");
s.last="Suzuki";s.age = 33;
s.gpa = 3.33;
-
8/10/2019 Lecture on C Programming Structure and union
5/33
Accessing Members
Structure member
operator, or dot
operator For direct variables(.)
Structure pointer
operator For pointers (->)
struct student s2;
struct student*sPtr = s2;
printf("%s %d\n",
s1.last, s1.age);
/*Tanaka 22*/
printf("%s %d\n",
(*sPtr).last, sPtr->age);
/*Suzuki 33*/
*See complete program at accessingMembers.txt
http://localhost/var/www/apps/conversion/tmp/scratch_1/accessingMembers.txthttp://localhost/var/www/apps/conversion/tmp/scratch_1/accessingMembers.txt -
8/10/2019 Lecture on C Programming Structure and union
6/33
sizeof Operator
Returns the size in bytes of a data typechar 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(b) = 10 */
/* sizeof(c) = 4 sizeof(d) = 40 */
/* sizeof(e) = 8 sizeof(f) = 80 */
-
8/10/2019 Lecture on C Programming Structure and union
7/33
sizeof Operator
Examples with pointerschar 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(*p1) = 1 */
/* sizeof(p2) = 4 sizeof(*p2) = 4 */
/* sizeof(p3) = 4 sizeof(*p3) = 8 */
-
8/10/2019 Lecture on C Programming Structure and union
8/33
sizeof Operator
Examples with structuresstruct 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) = 40
sizeof(s2) = 40
sizeof(s3) = 4
sizeof(s4) = 400 */
-
8/10/2019 Lecture on C Programming Structure and union
9/33
sizeof Operator
struct student{
char first[20]; /* 20 bytes */
char *last; /* 4 bytes */
int age; /* 4 bytes */
double gpa; /* 8 bytes */}; /* total = 36 bytes ?? */
Structures may have extra padding,
because computers may store specific datatypes on certain memory boundariesSee complete program at sizeof.txt
http://localhost/var/www/apps/conversion/tmp/scratch_1/sizeof.txthttp://localhost/var/www/apps/conversion/tmp/scratch_1/sizeof.txt -
8/10/2019 Lecture on C Programming Structure and union
10/33
Structures & Functions You can pass a structure to a function by value
struct student s1={"Ted","Tanaka", 22, 2.22};
incAge1(s1.age);
printStudent(s1);
/* Name = Ted Tanaka
age = 22 gpa = 2.22 */
void incAge1(int a){ a++;}
void printStudent(struct student s){
printf("Name = %s %s \n age = %d gpa =%.2f\n",s.first, s.last, s.age, s.gpa);}
-
8/10/2019 Lecture on C Programming Structure and union
11/33
Structures & Functions
Or pass by reference (pointer to a structure)struct student s1={"Ted","Tanaka", 22, 2.22};
struct student *p = &s1;
incAge2(p);
printStudent(s1);
/* Name = Ted Tanaka
age = 23 gpa = 2.22 */
void incAge2(struct student *s){s->age++;}
See complete program at functions.txt
http://localhost/var/www/apps/conversion/tmp/scratch_1/functions.txthttp://localhost/var/www/apps/conversion/tmp/scratch_1/functions.txt -
8/10/2019 Lecture on C Programming Structure and union
12/33
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 */
See complete program at array.txt
http://localhost/var/www/apps/conversion/tmp/scratch_1/array.txthttp://localhost/var/www/apps/conversion/tmp/scratch_1/array.txt -
8/10/2019 Lecture on C Programming Structure and union
13/33
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;}
http://localhost/var/www/apps/conversion/tmp/scratch_1/exercise1.txthttp://localhost/var/www/apps/conversion/tmp/scratch_1/exercise1.txt -
8/10/2019 Lecture on C Programming Structure and union
14/33
typedef
Creates new data
type name
Integer used asa synonym for int
IntegerPtr used as
synonym for int *
(See program at typedef.txt)
typedef int Integer;
typedef int* IntegerPtr;
main(){Integer a = 10;IntegerPtr b = &a;a++;
printf("a = %d\n",a);
printf("b = %d\n",*b);}
/*a = 11
b = 11*/
http://localhost/var/www/apps/conversion/tmp/scratch_1/typedef.txthttp://localhost/var/www/apps/conversion/tmp/scratch_1/typedef.txt -
8/10/2019 Lecture on C Programming Structure and union
15/33
typedef
Creates synonyms
for previously
declared data typesCan be defined after
the struct
Or defined in onestatement
. . .
typedef structstudent Student;
typedef structstudent{char first[20];
char *last;int age;double gpa;
} Student;
-
8/10/2019 Lecture on C Programming Structure and union
16/33
typedef
Does not create anew type
Instead adds a newname for an exitingtype
Similar to #define,except it isinterpreted by thecompiler
typedef struct student
Student;
. . .
Student s;
s.first = "Fred";
s.last = "Tanaka";
s.age = 22;s.gpa = 2.22;
See program at typedef2.txt
http://localhost/var/www/apps/conversion/tmp/scratch_1/typedef.txthttp://localhost/var/www/apps/conversion/tmp/scratch_1/typedef.txt -
8/10/2019 Lecture on C Programming Structure and union
17/33
Example Program
See fig10_03.cfrom D & D textbook
Represents a deck of cards as an array of
structuresTo shuffle the cards, the program randomly
swaps the cards
Output displays 52 cards in two columnsNote the use of the dot operator (.) and the use
of typedef
http://localhost/var/www/apps/conversion/tmp/scratch_1/fig10_03.c.txthttp://localhost/var/www/apps/conversion/tmp/scratch_1/fig10_03.c.txt -
8/10/2019 Lecture on C Programming Structure and union
18/33
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);}
http://localhost/var/www/apps/conversion/tmp/scratch_1/exercise2.txthttp://localhost/var/www/apps/conversion/tmp/scratch_1/exercise2.txt -
8/10/2019 Lecture on C Programming Structure and union
19/33
Unions
Similar to struct
Contains multiple
data types Members share the
same data space
Can manipulatedifferent kinds ofdata in a single unitof storage
union number{
int x;
/*4 bytes*/double y;
/*8 bytes*/
char z[20];
/*20 bytes*/};
-
8/10/2019 Lecture on C Programming Structure and union
20/33
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,"1234567890123456789");
printf("%d\n %f\n%s\n\n", num.x, num.y,num.z);
65
213568. . .2.000000
A
208632331
1.000001
zo
8757704170.000000
1234567890123456789
-
8/10/2019 Lecture on C Programming Structure and union
21/33
-
8/10/2019 Lecture on C Programming Structure and union
22/33
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
-
8/10/2019 Lecture on C Programming Structure and union
23/33
Bitwise Operators
A > B right shift
Shift the bits in A to the right B times Fill in 0s for unsigned, 0s for positive signed,and 1s for negative signed
Same as diving by a power of 2
-
8/10/2019 Lecture on C Programming Structure and union
24/33
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;
-
8/10/2019 Lecture on C Programming Structure and union
25/33
Bitwise Assignment Operators
A &= B bitwise AND assignment operator
Equivalent to A = A & B
A |= B bitwise OR assignment operator
A ^= B bitwise exclusive OR assignment
operator
A = B right shift assignment operator
-
8/10/2019 Lecture on C Programming Structure and union
26/33
Displaying Bits
See fig10_07.cin D & D textbook (slightlyaltered)
Prints the binary representation of 32-bitunsigned integers
Uses the AND operator and a operand called amask
Mask = an integer value with specific bits set to 1 Used to hide some bits & select other bits
A zero (0) ANDed with anything produces zero (0)
A one (1) ANDed with anything produces itself
http://localhost/var/www/apps/conversion/tmp/scratch_1/fig10_07.c.txthttp://localhost/var/www/apps/conversion/tmp/scratch_1/fig10_07.c.txt -
8/10/2019 Lecture on C Programming Structure and union
27/33
Left & Right Shifting Example
See fig10_13.c in D & D textbook (slightly
altered)
Shifts a signed integer to the left & to the right
Note that this is the same as multiplying or
dividing by 16 (24 = 16)
Note that the right shift preserves the sign ofthe number
http://localhost/var/www/apps/conversion/tmp/scratch_1/fig10_13.c.txthttp://localhost/var/www/apps/conversion/tmp/scratch_1/fig10_13.c.txt -
8/10/2019 Lecture on C Programming Structure and union
28/33
Bit Fields
Used to save space
Stores data by byte
Must use typeunsigned int
struct bitNum{unsigned a:1;/*1 bit*/
unsigned b:2;/*2 bits*/unsigned c:3;/*3 bits*/
unsigned d:4;/*4 bits*/
};
-
8/10/2019 Lecture on C Programming Structure and union
29/33
/*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 atbitFields.txt)
http://localhost/var/www/apps/conversion/tmp/scratch_1/bitFields.txthttp://localhost/var/www/apps/conversion/tmp/scratch_1/bitFields.txt -
8/10/2019 Lecture on C Programming Structure and union
30/33
Bit Field Card Example
See fig10_16.c in D & D textbook
Saves space by using bit fields
Since color is either black (1) or red (0), onlyneed one bit to store it
Suit has only 4 values, so can be stored in 2 bits
Face has values 0 (Ace) to 12 (King), so can bestored in 4 bits (15 possible values)
http://localhost/var/www/apps/conversion/tmp/scratch_1/fig10_16.c.txthttp://localhost/var/www/apps/conversion/tmp/scratch_1/fig10_16.c.txt -
8/10/2019 Lecture on C Programming Structure and union
31/33
/*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);
}
http://localhost/var/www/apps/conversion/tmp/scratch_1/exercise4.txthttp://localhost/var/www/apps/conversion/tmp/scratch_1/exercise4.txt -
8/10/2019 Lecture on C Programming Structure and union
32/33
Enumeration Constants
Set of integer constants
represented by
identifiers Values start with 0
(unless otherwise
specified) and
increment by 1
See fig10_18.cin D &
D textbook
enum months { JAN
= 1, FEB, MAR,
APR, MAY, JUN,
JUL, AUG, SEP,OCT, NOV, DEC };
http://localhost/var/www/apps/conversion/tmp/scratch_1/fig10_18.c.txthttp://localhost/var/www/apps/conversion/tmp/scratch_1/fig10_18.c.txt -
8/10/2019 Lecture on C Programming Structure and union
33/33
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