Microsoft PowerPoint – 11_Structures.pptx
1
CS 2211
Systems Programming
Part Ten: Structures
1
Address Label Label Address Value
MEMORY MAPPING – STRUCTURE
397 – 398 – 399 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
400 – 401 – 402 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
403 – 404 – 405 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
406 – 407 – 408 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
409 – 410 – 411 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
412 – 413 – 414 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
415 – 416 – 417 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
418 – 419 – 420 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
421 – 422 – 423 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
Address Label Label Address Value
MEMORY MAPPING – STRUCTURE
397 – 398 – 399 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
400 – 401 – 402 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
403 – 404 – 405 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
406 – 407 – 408 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
409 – 410 – 411 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
412 – 413 – 414 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
415 – 416 – 417 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
418 – 419 – 420 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
421 – 422 – 423 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
/* “person” is name for structure type */
struct person {
char first[32]; /* 1st field is array of char */
char last[32]; /* 2nd field is array of char */
int year; /* 3rd field is int */
double ppg; /* 4th field is double */
}; /* ending ; */
/* means end of structure type definition */
– a construct to group together dissimilar
variables under one name
Address Label Label Address Value
MEMORY MAPPING – STRUCTURE
397 – 398 – 399 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
400 – 401 – 402 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
403 – 404 – 405 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
406 – 407 – 408 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
409 – 410 – 411 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
412 – 413 – 414 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
415 – 416 – 417 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
418 – 419 – 420 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
421 – 422 – 423 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
/* “person” is name for structure type */
struct person {
char first[32]; /* 1st field is array of char */
char last[32]; /* 2nd field is array of char */
int year; /* 3rd field is int */
double ppg; /* 4th field is double */
}; /* ending ; */
/* means end of structure type definition */
– a construct to group together
dissimilar variables under one name
NOTE:
This code does NOT create a variable
(similar to Class declaration in OOP)
– grandfather of this concept
– this is ONLY a template for a variable declaration.
– this creates a USER DEFINED variable type
1 2
3 4
2
Address Label Label Address Value
MEMORY MAPPING – STRUCTURE
397 – 398 – 399 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
400 – 401 – 402 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
403 – 404 – 405 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
406 – 407 – 408 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
409 – 410 – 411 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
412 – 413 – 414 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
415 – 416 – 417 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
418 – 419 – 420 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
421 – 422 – 423 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
/* “person” is name for structure type */
struct person {
char first[32]; /* 1st field is array of char */
char last[32]; /* 2nd field is array of char */
int year; /* 3rd field is int */
double ppg; /* 4th field is double */
}; /* ending ; */
/* means end of structure type definition */
int i;
struct person teacher;
– a construct to group together
dissimilar variables under one name
creates an integer variable (space to store an integer) i
– and –
creates a person variable (space to store two character arrays,
and integer and a double) teacher.
MEMORY MAPPING – STRUCTURE
/* “person” is name for structure type */
struct person {
char first[32]; /* 1st field is array of char */
char last[32]; /* 2nd field is array of char */
int year; /* 3rd field is int */
double ppg; /* 4th field is double */
}; /* ending ; */
/* means end of structure type definition */
Address Label Label Address Value
/* “person” is name for structure type */
struct person {
char first[32]; /* 1st field is array of char */
char last[32]; /* 2nd field is array of char */
int year; /* 3rd field is int */
double ppg; /* 4th field is double */
}; /* ending ; */
/* means end of structure type definition */
struct person teacher;
MEMORY MAPPING – STRUCTURE
– memory 400 to 475
– the struct person
takes up (requires)
76 bytes of data
Address Label Label Address Value
teacher.first teacher
teacher.first[0]
400 –
teacher.first[1] 401 –
teacher.first[2] 402 –
teacher.first[3] 403 –
teacher.first[5] 404 –
teacher.first[6] – [31] 405 – 431
teacher.last teacher.last[0] 432 –
teacher.last[1] 433 –
teacher.last[2] 434 –
teacher.last[3] 435 –
teacher.last[4] 436 –
teacher.last[5] 437 –
teacher.last[5] 438 –
teacher.last[6] – [31] 439 – 463
teacher.year 464 – 467
teacher.ppg 468 – 475
note:
teacher
teacher.first
&teacher.first[0]
all returns the address 400
MEMORY MAPPING – STRUCTURE
/* “person” is name for structure type */
struct person {
char first[32]; /* 1st field is array of char */
char last[32]; /* 2nd field is array of char */
int year; /* 3rd field is int */
double ppg; /* 4th field is double */
}; /* ending ; */
/* means end of structure type definition */
struct person teacher;
teacher.year=2005;
teacher.ppg=10.4;
strcpy(teacher.first,”Sam”);
strcpy(teacher.last,”Maggs”);
Address Label Label Address Value
teacher.first teacher
teacher.first[0]
400 –
S
teacher.first[1] 401 – a
teacher.first[2] 402 – m
teacher.first[3] 403 – \0
teacher.first[5] 404 –
teacher.first[6] – [31] 405 – 431
teacher.last teacher.last[0] 432 – M
teacher.last[1] 433 – a
teacher.last[2] 434 – g
teacher.last[3] 435 – g
teacher.last[4] 436 – s
teacher.last[5] 437 – \0
teacher.last[5] 438 –
teacher.last[6] – [31] 439 – 463
teacher.year 464 – 467 2005
teacher.ppg 468 – 475 10.4
5 6
7 8
3
MEMORY MAPPING – STRUCTURE
/* “person” is name for structure type */
struct person {
char first[32]; /* 1st field is array of char */
char last[32]; /* 2nd field is array of char */
int year; /* 3rd field is int */
double ppg; /* 4th field is double */
}; /* ending ; */
/* means end of structure type definition */
struct person teacher;
teacher.year=2005;
teacher.ppg=10.4;
strcpy(teacher.first,”Sam”);
strcpy(teacher.last,”Maggs”);
Address Label Label Address Value
teacher.first teacher
teacher.first[0]
400 –
A
teacher.first[1] 401 – d
teacher.first[2] 402 – a
teacher.first[3] 403 – m
teacher.first[5] 404 – \0
teacher.first[6] – [31] 405 – 431
teacher.last teacher.last[0] 432 – H
teacher.last[1] 433 – o
teacher.last[2] 434 – o
teacher.last[3] 435 – v
teacher.last[4] 436 – e
teacher.last[5] 437 – r
teacher.last[5] 438 –
teacher.last[6] – [31] 439 – 463
teacher.year 464 – 467 2005
teacher.ppg 468 – 475 10.4
notice memory location 400
teacher // refers to the entire structure starting at 400
teacher.first // refers to the character array starting at 400
teacher.first[0] // refers to the single character byte at 400
Having a label that refers to the entire structure
is used in two ways:
1.) allows assignment between two identical structure types:
struct person mailman, teacher;
mailman = teacher;
// a byte by bytes transference of values (including nulls)
2.) passing a structure as a parameter to a function.
// allows the easy transfer of large amounts of data
MEMORY MAPPING – STRUCTURE
/* “person” is name for structure type */
struct person {
char first[32]; /* 1st field is array of char */
char last[32]; /* 2nd field is array of char */
int year; /* 3rd field is int */
double ppg; /* 4th field is double */
}; /* ending ; */
/* means end of structure type definition */
struct person teacher;
teacher.year=2005;
teacher.ppg=10.4;
strcpy(teacher.first,”Sam”);
strcpy(teacher.last,”Maggs”);
Address Label Label Address Value
teacher.first teacher
teacher.first[0]
400 –
A
teacher.first[1] 401 – d
teacher.first[2] 402 – a
teacher.first[3] 403 – m
teacher.first[5] 404 – \0
teacher.first[6] – [31] 405 – 431
teacher.last teacher.last[0] 432 – H
teacher.last[1] 433 – o
teacher.last[2] 434 – o
teacher.last[3] 435 – v
teacher.last[4] 436 – e
teacher.last[5] 437 – r
teacher.last[5] 438 – \0
teacher.last[6] – [31] 439 – 463
teacher.year 464 – 467 2005
teacher.ppg 468 – 475 10.4
– passing a structure as a parameter to a function.
// allows the easy transfer of large amounts of data
DisplayStats(struct person Input) {
Input.first = “Dunsul”;
}
/* “person” is name for structure type */
struct person {
char first[32]; /* 1st field is array of char */
char last[32]; /* 2nd field is array of char */
int year; /* 3rd field is int */
double ppg; /* 4th field is double */
}; /* ending ; */
/* means end of structure type definition */
struct person teacher;
teacher.year=2005;
teacher.ppg=10.4;
strcpy(teacher.first,”Sam”);
strcpy(teacher.last,”Maggs”);
DisplayStats(teacher);
printf(“%s\n”,teacher.first);
SIDE BAR:
– is the a call
by value
-or-
a call by
reference ?
MEMORY MAPPING – STRUCTURE
/* “person” is name for structure type */
struct person {
char first[32]; /* 1st field is array of char */
char last[32]; /* 2nd field is array of char */
int year; /* 3rd field is int */
double ppg; /* 4th field is double */
}; /* ending ; */
/* means end of structure type definition */
struct person teacher;
teacher.year=2005;
teacher.ppg=10.4;
strcpy(teacher.first,”Sam”);
strcpy(teacher.last,”Maggs”);
Address Label Label Address Value
teacher.first teacher
teacher.first[0]
400 –
A
teacher.first[1] 401 – d
teacher.first[2] 402 – a
teacher.first[3] 403 – m
teacher.first[5] 404 – \0
teacher.first[6] – [31] 405 – 431
teacher.last teacher.last[0] 432 – H
teacher.last[1] 433 – o
teacher.last[2] 434 – o
teacher.last[3] 435 – v
teacher.last[4] 436 – e
teacher.last[5] 437 – r
teacher.last[5] 438 – \0
teacher.last[6] – [31] 439 – 463
teacher.year 464 – 467 2005
teacher.ppg 468 – 475 10.4
– passing a structure as a parameter to a function.
// allows the easy transfer of large amounts of data
DisplayStats(struct person Input) {
Input.first = “Dunsul”;
}
/* “person” is name for structure type */
struct person {
char first[32]; /* 1st field is array of char */
char last[32]; /* 2nd field is array of char */
int year; /* 3rd field is int */
double ppg; /* 4th field is double */
}; /* ending ; */
/* means end of structure type definition */
struct person teacher;
teacher.year=2005;
teacher.ppg=10.4;
strcpy(teacher.first,”Sam”);
strcpy(teacher.last,”Maggs”);
DisplayStats(teacher);
printf(“%s\n”,teacher.first);
SIDE BAR:
– is the a call
by value
-or-
a call by
reference ?
Label Address Value
MEMORY MAPPING – STRUCTURE
/* “person” is name for structure type */
struct person {
char first[32]; /* 1st field is array of char */
char last[32]; /* 2nd field is array of char */
int year; /* 3rd field is int */
double ppg; /* 4th field is double */
}; /* ending ; */
/* means end of structure type definition */
struct person teacher;
teacher.year=2005;
teacher.ppg=10.4;
strcpy(teacher.first,”Sam”);
strcpy(teacher.last,”Maggs”);
Address Label Label Address Value
teacher.first teacher
teacher.first[0]
400 –
A
teacher.first[1] 401 – d
teacher.first[2] 402 – a
teacher.first[3] 403 – m
teacher.first[5] 404 – \0
teacher.first[6] – [31] 405 – 431
teacher.last teacher.last[0] 432 – H
teacher.last[1] 433 – o
teacher.last[2] 434 – o
teacher.last[3] 435 – v
teacher.last[4] 436 – e
teacher.last[5] 437 – r
teacher.last[5] 438 – \0
teacher.last[6] – [31] 439 – 463
teacher.year 464 – 467 2005
teacher.ppg 468 – 475 10.4
– passing a structure as a parameter to a function.
// allows the easy transfer of large amounts of data
DisplayStats(struct person Input) {
Input.first = “Dunsul”;
}
/* “person” is name for structure type */
struct person {
char first[32]; /* 1st field is array of char */
char last[32]; /* 2nd field is array of char */
int year; /* 3rd field is int */
double ppg; /* 4th field is double */
}; /* ending ; */
/* means end of structure type definition */
struct person teacher;
teacher.year=2005;
teacher.ppg=10.4;
strcpy(teacher.first,”Sam”);
strcpy(teacher.last,”Maggs”);
DisplayStats(teacher);
printf(“%s\n”,teacher.first);
SIDE BAR:
– is the a call
by value
-or-
a call by
reference ?
Label Address Value
teacher 400 – 475
9 10
11 12
4
MEMORY MAPPING – STRUCTURE
/* “person” is name for structure type */
struct person {
char first[32]; /* 1st field is array of char */
char last[32]; /* 2nd field is array of char */
int year; /* 3rd field is int */
double ppg; /* 4th field is double */
}; /* ending ; */
/* means end of structure type definition */
struct person teacher;
teacher.year=2005;
teacher.ppg=10.4;
strcpy(teacher.first,”Sam”);
strcpy(teacher.last,”Maggs”);
Address Label Label Address Value
teacher.first teacher
teacher.first[0]
400 –
A
teacher.first[1] 401 – d
teacher.first[2] 402 – a
teacher.first[3] 403 – m
teacher.first[5] 404 – \0
teacher.first[6] – [31] 405 – 431
teacher.last teacher.last[0] 432 – H
teacher.last[1] 433 – o
teacher.last[2] 434 – o
teacher.last[3] 435 – v
teacher.last[4] 436 – e
teacher.last[5] 437 – r
teacher.last[5] 438 – \0
teacher.last[6] – [31] 439 – 463
teacher.year 464 – 467 2005
teacher.ppg 468 – 475 10.4
– passing a structure as a parameter to a function.
// allows the easy transfer of large amounts of data
DisplayStats(struct person Input) {
Input.first = “Dunsul”;
}
/* “person” is name for structure type */
struct person {
char first[32]; /* 1st field is array of char */
char last[32]; /* 2nd field is array of char */
int year; /* 3rd field is int */
double ppg; /* 4th field is double */
}; /* ending ; */
/* means end of structure type definition */
struct person teacher;
teacher.year=2005;
teacher.ppg=10.4;
strcpy(teacher.first,”Sam”);
strcpy(teacher.last,”Maggs”);
DisplayStats(teacher);
printf(“%s\n”,teacher.first);
SIDE BAR:
– is the a call
by value
-or-
a call by
reference ?
Label Address Value
teacher 400 – 475 [original]
MEMORY MAPPING – STRUCTURE
/* “person” is name for structure type */
struct person {
char first[32]; /* 1st field is array of char */
char last[32]; /* 2nd field is array of char */
int year; /* 3rd field is int */
double ppg; /* 4th field is double */
}; /* ending ; */
/* means end of structure type definition */
struct person teacher;
teacher.year=2005;
teacher.ppg=10.4;
strcpy(teacher.first,”Sam”);
strcpy(teacher.last,”Maggs”);
– passing a structure as a parameter to a function.
// allows the easy transfer of large amounts of data
DisplayStats(struct person Input) {
Input.first = “Dunsul”;
}
/* “person” is name for structure type */
struct person {
char first[32]; /* 1st field is array of char */
char last[32]; /* 2nd field is array of char */
int year; /* 3rd field is int */
double ppg; /* 4th field is double */
}; /* ending ; */
/* means end of structure type definition */
struct person teacher;
teacher.year=2005;
teacher.ppg=10.4;
strcpy(teacher.first,”Sam”);
strcpy(teacher.last,”Maggs”);
DisplayStats(teacher);
printf(“%s\n”,teacher.first);
SIDE BAR:
– is the a call
by value
-or-
a call by
reference ?
Label Address Value
teacher 400 – 475 [original]
Address Label Label Address Value
teacher.first teacher
teacher.first[0]
400 –
S
teacher.first[1] 401 – a
teacher.first[2] 402 – m
teacher.first[3] 403 – \0
teacher.first[5] 404 –
teacher.first[6] – [31] 405 – 431
teacher.last teacher.last[0] 432 – M
teacher.last[1] 433 – a
teacher.last[2] 434 – g
teacher.last[3] 435 – g
teacher.last[4] 436 – s
teacher.last[5] 437 – \0
teacher.last[5] 438 –
teacher.last[6] – [31] 439 – 463
teacher.year 464 – 467 2005
teacher.ppg 468 – 475 10.4
MEMORY MAPPING – STRUCTURE
/* “person” is name for structure type */
struct person {
char first[32]; /* 1st field is array of char */
char last[32]; /* 2nd field is array of char */
int year; /* 3rd field is int */
double ppg; /* 4th field is double */
}; /* ending ; */
/* means end of structure type definition */
struct person teacher;
teacher.year=2005;
teacher.ppg=10.4;
strcpy(teacher.first,”Sam”);
strcpy(teacher.last,”Maggs”);
Address Label Label Address Value
teacher.first teacher
teacher.first[0]
400 –
A
teacher.first[1] 401 – d
teacher.first[2] 402 – a
teacher.first[3] 403 – m
teacher.first[5] 404 – \0
teacher.first[6] – [31] 405 – 431
teacher.last teacher.last[0] 432 – H
teacher.last[1] 433 – o
teacher.last[2] 434 – o
teacher.last[3] 435 – v
teacher.last[4] 436 – e
teacher.last[5] 437 – r
teacher.last[5] 438 – \0
teacher.last[6] – [31] 439 – 463
teacher.year 464 – 467 2005
teacher.ppg 468 – 475 10.4
– passing a structure as a parameter to a function.
// allows the easy transfer of large amounts of data
DisplayStats(struct person Input) {
Input.first = “Dunsul”;
}
/* “person” is name for structure type */
struct person {
char first[32]; /* 1st field is array of char */
char last[32]; /* 2nd field is array of char */
int year; /* 3rd field is int */
double ppg; /* 4th field is double */
}; /* ending ; */
/* means end of structure type definition */
struct person teacher;
teacher.year=2005;
teacher.ppg=10.4;
strcpy(teacher.first,”Sam”);
strcpy(teacher.last,”Maggs”);
DisplayStats(teacher);
printf(“%s\n”,teacher.first);
SIDE BAR:
– is the a call
by value
-or-
a call by
reference ?
Label Address Value
teacher 400 – 475 [original]
Input 710 – 785 [ copy ]
MEMORY MAPPING – STRUCTURE
/* “person” is name for structure type */
struct person {
char first[32]; /* 1st field is array of char */
char last[32]; /* 2nd field is array of char */
int year; /* 3rd field is int */
double ppg; /* 4th field is double */
}; /* ending ; */
/* means end of structure type definition */
struct person teacher;
teacher.year=2005;
teacher.ppg=10.4;
strcpy(teacher.first,”Sam”);
strcpy(teacher.last,”Maggs”);
Address Label Label Address Value
teacher.first teacher
teacher.first[0]
400 –
A
teacher.first[1] 401 – d
teacher.first[2] 402 – a
teacher.first[3] 403 – m
teacher.first[5] 404 – \0
teacher.first[6] – [31] 405 – 431
teacher.last teacher.last[0] 432 – H
teacher.last[1] 433 – o
teacher.last[2] 434 – o
teacher.last[3] 435 – v
teacher.last[4] 436 – e
teacher.last[5] 437 – r
teacher.last[5] 438 – \0
teacher.last[6] – [31] 439 – 463
teacher.year 464 – 467 2005
teacher.ppg 468 – 475 10.4
– passing a structure as a parameter to a function.
// allows the easy transfer of large amounts of data
DisplayStats(struct person Input) {
Input.first = “Dunsul”;
}
/* “person” is name for structure type */
struct person {
char first[32]; /* 1st field is array of char */
char last[32]; /* 2nd field is array of char */
int year; /* 3rd field is int */
double ppg; /* 4th field is double */
}; /* ending ; */
/* means end of structure type definition */
struct person teacher;
teacher.year=2005;
teacher.ppg=10.4;
strcpy(teacher.first,”Sam”);
strcpy(teacher.last,”Maggs”);
DisplayStats(teacher);
printf(“%s\n”,teacher.first);
SIDE BAR:
– is the a call
by value
-or-
a call by
reference ?
Label Address Value
teacher 400 – 475 [original]
Input 710 – 785 [ copy ]
OUTPUT:
13 14
15 16
5
MEMORY MAPPING – STRUCTURE
/* “person” is name for structure type */
struct person {
char first[32]; /* 1st field is array of char */
char last[32]; /* 2nd field is array of char */
int year; /* 3rd field is int */
double ppg; /* 4th field is double */
}; /* ending ; */
/* means end of structure type definition */
struct person teacher;
teacher.year=2005;
teacher.ppg=10.4;
strcpy(teacher.first,”Sam”);
strcpy(teacher.last,”Maggs”);
Address Label Label Address Value
teacher.first teacher
teacher.first[0]
400 –
A
teacher.first[1] 401 – d
teacher.first[2] 402 – a
teacher.first[3] 403 – m
teacher.first[5] 404 – \0
teacher.first[6] – [31] 405 – 431
teacher.last teacher.last[0] 432 – H
teacher.last[1] 433 – o
teacher.last[2] 434 – o
teacher.last[3] 435 – v
teacher.last[4] 436 – e
teacher.last[5] 437 – r
teacher.last[5] 438 – \0
teacher.last[6] – [31] 439 – 463
teacher.year 464 – 467 2005
teacher.ppg 468 – 475 10.4
– passing a structure as a parameter to a function.
// allows the easy transfer of large amounts of data
DisplayStats(struct person Input) {
Input.first = “Dunsul”;
}
/* “person” is name for structure type */
struct person {
char first[32]; /* 1st field is array of char */
char last[32]; /* 2nd field is array of char */
int year; /* 3rd field is int */
double ppg; /* 4th field is double */
}; /* ending ; */
/* means end of structure type definition */
struct person teacher;
teacher.year=2005;
teacher.ppg=10.4;
strcpy(teacher.first,”Sam”);
strcpy(teacher.last,”Maggs”);
DisplayStats(teacher);
printf(“%s\n”,teacher.first);
SIDE BAR:
– is the a call
by value
-or-
a call by
reference ?
Label Address Value
teacher 400 – 475 [original]
Input 710 – 785 [ copy ]
HOW TO CHANGE
SO THE LINE PRINTS OUT:
Dunsul
(lab? or assignment?)
18
Structures in C
END OF PART 1
Address Label Label Address Value
MEMORY MAPPING – STRUCTURE
397 – 398 – 399 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
400 – 401 – 402 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
403 – 404 – 405 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
406 – 407 – 408 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
409 – 410 – 411 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
412 – 413 – 414 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
415 – 416 – 417 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
418 – 419 – 420 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
421 – 422 – 423 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
struct name {
char first[30];
char last[30];
};
struct person {
int age;
float ppg;
struct name title; /* nested structure */
}; /* ‘name’ must be defined above */
/* the structure person now has a nested structure called name */
– structures can be nested
– so that a field inside a structure can be
another structure
MEMORY MAPPING – STRUCTURE
struct name {
char first[30];
char last[30];
};
struct person {
int age;
float ppg;
struct name title;
};
Address Label Label Address Value
17 18
19 20
6
MEMORY MAPPING – STRUCTURE
struct name {
char first[30];
char last[30];
};
struct person {
int age;
float ppg;
struct name title;
};
struct person boss;
Address Label Label Address Value
boss boss.age 400 – 403
boss.ppg 404 – 407
boss.title.first boss.title boss.title.first[0] 408 –
boss.title.first[1] 409 –
boss.title.first[2] 410 –
boss.title.first[3] 411 –
boss.title.first[4] 412 –
boss.title.first[5] – [31] 413 – 439
boss.title.last boss.title.last[0] 440 –
boss.title.last[1] 441 –
boss.title.last[2] 442 –
boss.title.last[3] 443 –
boss.title.last[4] 444 –
boss.title.last[5] 445 –
boss.title.last[6] – [31] 446 – 471
MEMORY MAPPING – STRUCTURE
struct name {
char first[30];
char last[30];
};
struct person {
int age;
float ppg;
struct name title;
};
struct person boss;
/* fill in some values
for the nested
structure */
boss.age=40;
Address Label Label Address Value
boss boss.age 400 – 403 40
boss.ppg 404 – 407
boss.title.first boss.title boss.title.first[0] 408 –
boss.title.first[1] 409 –
boss.title.first[2] 410 –
boss.title.first[3] 411 –
boss.title.first[4] 412 –
boss.title.first[5] – [31] 413 – 439
boss.title.last boss.title.last[0] 440 –
boss.title.last[1] 441 –
boss.title.last[2] 442 –
boss.title.last[3] 443 –
boss.title.last[4] 444 –
boss.title.last[5] 445 –
boss.title.last[6] – [31] 446 – 471
MEMORY MAPPING – STRUCTURE
struct name {
char first[30];
char last[30];
};
struct person {
int age;
float ppg;
struct name title;
};
struct person boss;
/* fill in some values
for the nested
structure */
boss.age=40;
boss.ppg=0.1;
Address Label Label Address Value
boss boss.age 400 – 403 40
boss.ppg 404 – 407 0.1
boss.title.first boss.title boss.title.first[0] 408 –
boss.title.first[1] 409 –
boss.title.first[2] 410 –
boss.title.first[3] 411 –
boss.title.first[4] 412 –
boss.title.first[5] – [31] 413 – 439
boss.title.last boss.title.last[0] 440 –
boss.title.last[1] 441 –
boss.title.last[2] 442 –
boss.title.last[3] 443 –
boss.title.last[4] 444 –
boss.title.last[5] 445 –
boss.title.last[6] – [31] 446 – 471
MEMORY MAPPING – STRUCTURE
Address Label Label Address Value
boss boss.age 400 – 403 40
boss.ppg 404 – 407 0.1
boss.title.first boss.title boss.title.first[0] 408 – D
boss.title.first[1] 409 – e
boss.title.first[2] 410 – a
boss.title.first[3] 411 – n
boss.title.first[4] 412 – \0
boss.title.first[5] – [31] 413 – 439
boss.title.last boss.title.last[0] 440 –
boss.title.last[1] 441 –
boss.title.last[2] 442 –
boss.title.last[3] 443 –
boss.title.last[4] 444 –
boss.title.last[5] 445 –
boss.title.last[6] – [31] 446 – 471
struct name {
char first[30];
char last[30];
};
struct person {
int age;
float ppg;
struct name title;
};
struct person boss;
/* fill in some values
for the nested
structure */
boss.age=40;
boss.ppg=0.1;
strcpy(boss.title.first,”Dean”);
21 22
23 24
7
MEMORY MAPPING – STRUCTURE
Address Label Label Address Value
boss boss.age 400 – 403 40
boss.ppg 404 – 407 0.1
boss.title.first boss.title boss.title.first[0] 408 – D
boss.title.first[1] 409 – e
boss.title.first[2] 410 – a
boss.title.first[3] 411 – n
boss.title.first[4] 412 – \0
boss.title.first[5] – [31] 413 – 439
boss.title.last boss.title.last[0] 440 – S
boss.title.last[1] 441 – m
boss.title.last[2] 442 – i
boss.title.last[3] 443 – t
boss.title.last[4] 444 – h
boss.title.last[5] 445 – \0
boss.title.last[6] – [31] 446 – 471
struct name {
char first[30];
char last[30];
};
struct person {
int age;
float ppg;
struct name title;
};
struct person boss;
/* fill in some values
for the nested
structure */
boss.age=40;
boss.ppg=0.1;
strcpy(boss.title.first,”Dean”);
strcpy(boss.title.last,”Smith”);
26
Structures in C
END OF PART 2
MEMORY MAPPING – STRUCTURE
397 – 398 – 399 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
400 – 401 – 402 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
403 – 404 – 405 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
406 – 407 – 408 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
409 – 410 – 411 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
412 – 413 – 414 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
415 – 416 – 417 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
418 – 419 – 420 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
421 – 422 – 423 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
struct fraction {
int wP;
int fP;
};
struct fraction f[3], *g;
POINTERS and STRUCTURES
– can be stored as an address in a pointer variable
– just like any other variable
Address Label Label Address Value
f f[0] f[0]. wP 400 – 403
f[0]. fP 404 – 407
f[1] f[1]. wP 408 – 411
f[1]. fP 412 – 415
f[2] f[2]. wP 416 – 419
f[2]. fP 420 – 423
g 424 – 427
MEMORY MAPPING – STRUCTURE
397 – 398 – 399 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
400 – 401 – 402 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
403 – 404 – 405 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
406 – 407 – 408 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
409 – 410 – 411 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
412 – 413 – 414 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
415 – 416 – 417 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
418 – 419 – 420 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
421 – 422 – 423 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
POINTERS and STRUCTURES
– can be stored as an address in a pointer variable
– just like any other variable
Address Label Label Address Value
f f[0] f[0]. wP 400 – 403 3
f[0]. fP 404 – 407 7
f[1] f[1]. wP 408 – 411
f[1]. fP 412 – 415
f[2] f[2]. wP 416 – 419
f[2]. fP 420 – 423
g 424 – 427 400
struct fraction {
int wP;
int fP;
};
struct fraction f[3], *g;
f[0].wP = 3;
f[0].fP = 7;
g = &(f[0]);
25 26
27 28
8
struct fraction {
int wP;
int fP;
};
struct fraction f[3], *g;
f[0].wP = 3;
f[0].fP = 7;
g = &(f[0]);
g++;
/* g now
contains
400 + 8
408 */
MEMORY MAPPING – STRUCTURE
397 – 398 – 399 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
400 – 401 – 402 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
403 – 404 – 405 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
406 – 407 – 408 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
409 – 410 – 411 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
412 – 413 – 414 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
415 – 416 – 417 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
418 – 419 – 420 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
421 – 422 – 423 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
POINTERS and STRUCTURES
– can be stored as an address in a pointer variable
– just like any other variable
Address Label Label Address Value
f f[0] f[0]. wP 400 – 403 3
f[0]. fP 404 – 407 7
f[1] f[1]. wP 408 – 411
f[1]. fP 412 – 415
f[2] f[2]. wP 416 – 419
f[2]. fP 420 – 423
g 424 – 427 408
struct fraction {
int wP;
int fP;
};
struct fraction f[3], *g;
f[0].wP= 3;
f[0].fP= 7;
g = &(f[0]);
g++;
(*g).wP = 5;
g->fP = 11;
MEMORY MAPPING – STRUCTURE
397 – 398 – 399 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
400 – 401 – 402 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
403 – 404 – 405 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
406 – 407 – 408 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
409 – 410 – 411 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
412 – 413 – 414 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
415 – 416 – 417 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
418 – 419 – 420 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
421 – 422 – 423 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
POINTERS and STRUCTURES
– can be stored as an address in a pointer variable
– just like any other variable
Address Label Label Address Value
f f[0] f[0]. wP 400 – 403 3
f[0]. fP 404 – 407 7
f[1] f[1]. wP 408 – 411 5
f[1]. fP 412 – 415 11
f[2] f[2]. wP 416 – 419
f[2]. fP 420 – 423
g 424 – 427 408
struct fraction {
int wP;
int fP;
};
struct fraction f[3], *g;
f[0].wP = 3;
f[0].fP = 7;
g = &(f[0]);
g++;
(*g).wP = 5;
g->fP = 11;
MEMORY MAPPING – STRUCTURE
397 – 398 – 399 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
400 – 401 – 402 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
403 – 404 – 405 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
406 – 407 – 408 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
409 – 410 – 411 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
412 – 413 – 414 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
415 – 416 – 417 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
418 – 419 – 420 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
421 – 422 – 423 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
POINTERS and STRUCTURES
– can be stored as an address in a pointer variable
– just like any other variable
Address Label Label Address Value
f f[0] f[0]. wP 400 – 403 3
f[0]. fP 404 – 407 7
f[1] f[1]. wP 408 – 411 5
f[1]. fP 412 – 415 11
f[2] f[2]. wP 416 – 419
f[2]. fP 420 – 423
g 424 – 427 408
– two ways of specifying the field to use.
what we have already seen:
(*g).wP = 5;
// * (at the address) of g go to the field at wP
C also has a direction symbol
->
g->fP = 11;
– which can be used on addresses of structures
go to the structure pointed to by g
go to the fP variable of that structure.
MEMORY MAPPING – STRUCTURE
397 – 398 – 399 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
400 – 401 – 402 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
403 – 404 – 405 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
406 – 407 – 408 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
409 – 410 – 411 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
412 – 413 – 414 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
415 – 416 – 417 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
418 – 419 – 420 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
421 – 422 – 423 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
struct fraction {
int wP;
int fP;
} ;
struct fraction fract;
POINTERS and STRUCTURES
– a structure can be declared.
Address
Label
Label Address Value
fract.wP 400 – 403
fract.fP 404 – 407
29 30
31 32
9
MEMORY MAPPING – STRUCTURE
397 – 398 – 399 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
400 – 401 – 402 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
403 – 404 – 405 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
406 – 407 – 408 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
409 – 410 – 411 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
412 – 413 – 414 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
415 – 416 – 417 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
418 – 419 – 420 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
421 – 422 – 423 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
struct fraction {
int wP;
int fP;
} ;
struct fraction fract;
fract.wp = 7;
fract.fP = 3;
POINTERS and STRUCTURES
– a structure can be declared.
Address
Label
Label Address Value
fract.wP 400 – 403 7
fract.fP 404 – 407 3
MEMORY MAPPING – STRUCTURE
397 – 398 – 399 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
400 – 401 – 402 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
403 – 404 – 405 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
406 – 407 – 408 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
409 – 410 – 411 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
412 – 413 – 414 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
415 – 416 – 417 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
418 – 419 – 420 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
421 – 422 – 423 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
struct fraction {
int wP;
int fP;
} fract;
POINTERS and STRUCTURES
– a structure can be declared.
Address
Label
Label Address Value
fract.wP 400 – 403
fract.fP 404 – 407
MEMORY MAPPING – STRUCTURE
397 – 398 – 399 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
400 – 401 – 402 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
403 – 404 – 405 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
406 – 407 – 408 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
409 – 410 – 411 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
412 – 413 – 414 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
415 – 416 – 417 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
418 – 419 – 420 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
421 – 422 – 423 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
struct fraction {
int wP;
int fP;
} fract;
fract.wp = 7;
fract.fP = 3;
POINTERS and STRUCTURES
– a structure can be declared.
Address
Label
Label Address Value
fract.wP 400 – 403 7
fract.fP 404 – 407 3
MEMORY MAPPING – STRUCTURE
397 – 398 – 399 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
400 – 401 – 402 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
403 – 404 – 405 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
406 – 407 – 408 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
409 – 410 – 411 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
412 – 413 – 414 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
415 – 416 – 417 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
418 – 419 – 420 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
421 – 422 – 423 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
struct fraction {
int wP;
int fP;
} fract;
struct fraction newNum;
fract.wp = 7;
fract.fP = 3;
POINTERS and STRUCTURES
– a structure can be declared.
– and/or declared later
Address
Label
Label Address Value
fract.wP 400 – 403
fract.fP 404 – 407
newNum.wP 408 – 411
newNum.fP 412 – 415
33 34
35 36
10
MEMORY MAPPING – STRUCTURE
397 – 398 – 399 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
400 – 401 – 402 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
403 – 404 – 405 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
406 – 407 – 408 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
409 – 410 – 411 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
412 – 413 – 414 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
415 – 416 – 417 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
418 – 419 – 420 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
421 – 422 – 423 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
struct fraction {
int wP;
int fP;
} fract;
struct fraction newNum;
fract.wp = 7;
fract.fP = 3;
newNum.wp = 2;
newNum.fP = 6;
POINTERS and STRUCTURES
– a structure can be declared.
Address
Label
Label Address Value
fract.wP 400 – 403 7
fract.fP 404 – 407 3
newNum.wP 408 – 411 2
newNum.fP 412 – 415 6
MEMORY MAPPING – STRUCTURE
397 – 398 – 399 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
400 – 401 – 402 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
403 – 404 – 405 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
406 – 407 – 408 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
409 – 410 – 411 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
412 – 413 – 414 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
415 – 416 – 417 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
418 – 419 – 420 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
421 – 422 – 423 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
typedef struct fraction {
int wP;
int fP;
} fract;
POINTERS and STRUCTURES
– a structure can be declared.
Address
Label
Label Address Value
notice:
– the typedef does
NOT allocate
memory for the
variable.
– fract is just the
alias for this
construct.
MEMORY MAPPING – STRUCTURE
397 – 398 – 399 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
400 – 401 – 402 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
403 – 404 – 405 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
406 – 407 – 408 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
409 – 410 – 411 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
412 – 413 – 414 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
415 – 416 – 417 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
418 – 419 – 420 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
421 – 422 – 423 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
typedef struct fraction {
int wP;
int fP;
} fract;
fract first, second;
POINTERS and STRUCTURES
– a structure can be declared.
Address
Label
Label Address Value
first.wP 400 – 403
first.fP 404 – 407
second.wP 408 – 411
second.fP 412 – 415
MEMORY MAPPING – STRUCTURE
397 – 398 – 399 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
400 – 401 – 402 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
403 – 404 – 405 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
406 – 407 – 408 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
409 – 410 – 411 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
412 – 413 – 414 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
415 – 416 – 417 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
418 – 419 – 420 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
421 – 422 – 423 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
typedef struct fraction {
int wP;
int fP;
} fract;
fract first, second;
first.wp = 7;
first.fP = 3;
second.wp = 2;
second.fP = 6;
POINTERS and STRUCTURES
– a structure can be declared.
Address
Label
Label Address Value
first.wP 400 – 403 7
first.fP 404 – 407 3
second.wP 408 – 411 2
second.fP 412 – 415 6
37 38
39 40
11
41
Structures in C
END OF PART 3
MEMORY MAPPING – STRUCTURE
397 – 398 – 399 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
400 – 401 – 402 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
403 – 404 – 405 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
406 – 407 – 408 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
409 – 410 – 411 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
412 – 413 – 414 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
415 – 416 – 417 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
418 – 419 – 420 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
421 – 422 – 423 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
struct fraction {
int wP;
int fP;
};
struct fraction f[3], *g;
POINTERS and STRUCTURES
– a pointer variable can also be used
to DYNAMICALLY allocate memory.
Address
Label
Label Address Value
f f[0] f[0]. wP 400 – 403
f[0]. fP 404 – 407
f[1] f[1]. wP 408 – 411
f[1]. fP 412 – 415
f[2] f[2]. wP 416 – 419
f[2]. fP 420 – 423
g 424 – 427
MEMORY MAPPING – STRUCTURE
397 – 398 – 399 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
400 – 401 – 402 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
403 – 404 – 405 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
406 – 407 – 408 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
409 – 410 – 411 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
412 – 413 – 414 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
415 – 416 – 417 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
418 – 419 – 420 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
421 – 422 – 423 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
struct fraction {
int wP;
int fP;
};
struct fraction f[3], *g;
g = (struct fraction *)malloc( sizeof(struct fraction));
POINTERS and STRUCTURES
– a pointer variable can also be used
to DYNAMICALLY allocate memory.
Address
Label
Label Address Value
f f[0] f[0]. wP 400 – 403
f[0]. fP 404 – 407
f[1] f[1]. wP 408 – 411
f[1]. fP 412 – 415
f[2] f[2]. wP 416 – 419
f[2]. fP 420 – 423
g 424 – 427 10100
{DM} 10100 – 10107
MEMORY MAPPING – STRUCTURE
397 – 398 – 399 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
400 – 401 – 402 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
403 – 404 – 405 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
406 – 407 – 408 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
409 – 410 – 411 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
412 – 413 – 414 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
415 – 416 – 417 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
418 – 419 – 420 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
421 – 422 – 423 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
struct fraction {
int wP;
int fP;
};
struct fraction f[3], *g;
g = (struct fraction *)malloc( sizeof(struct fraction));
POINTERS and STRUCTURES
– a pointer variable can also be used
to DYNAMICALLY allocate memory.
Address
Label
Label Address Value
f f[0] f[0]. wP 400 – 403
f[0]. fP 404 – 407
f[1] f[1]. wP 408 – 411
f[1]. fP 412 – 415
f[2] f[2]. wP 416 – 419
f[2]. fP 420 – 423
g 424 – 427 10100
{DM} 10100 – 10107
g = (struct fraction *)malloc( sizeof(struct fraction));
– what is happening here ?
– a struct of type fraction is how many bytes in size ?
– 8 bytes (2 integer values x 4 bytes)
– so: ( sizeof( struct fraction ) ) will return 8 bytes of memory
– malloc() returns a pointer of type void
UNLESS specifically type cast as above
– this tells the program the pointer references
a memory location of a structure of the type fraction
41 42
43 44
12
MEMORY MAPPING – STRUCTURE
397 – 398 – 399 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
400 – 401 – 402 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
403 – 404 – 405 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
406 – 407 – 408 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
409 – 410 – 411 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
412 – 413 – 414 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
415 – 416 – 417 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
418 – 419 – 420 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
421 – 422 – 423 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
struct fraction {
int wP;
int fP;
};
struct fraction f[3], *g;
g = (struct fraction *)malloc( sizeof(struct fraction));
g->wP=6;
(*g).fP=1;
POINTERS and STRUCTURES
– a pointer variable can also be used
to DYNAMICALLY allocate memory.
Address
Label
Label Address Value
f f[0] f[0]. wP 400 – 403
f[0]. fP 404 – 407
f[1] f[1]. wP 408 – 411
f[1]. fP 412 – 415
f[2] f[2]. wP 416 – 419
f[2]. fP 420 – 423
g 424 – 427 10100
g.wP 10100 – 10103 6
g.fP 10104 – 10107 1
MEMORY MAPPING – STRUCTURE
397 – 398 – 399 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
400 – 401 – 402 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
403 – 404 – 405 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
406 – 407 – 408 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
409 – 410 – 411 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
412 – 413 – 414 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
415 – 416 – 417 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
418 – 419 – 420 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
421 – 422 – 423 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
struct fraction {
int wP;
int fP;
};
struct fraction f[3], *g;
g = (struct fraction *)malloc( sizeof(struct fraction));
g->wP=6;
(*g).fP=1;
POINTERS and STRUCTURES
– a pointer variable can also be used
to DYNAMICALLY allocate memory.
Address
Label
Label Address Value
f f[0] f[0]. wP 400 – 403
f[0]. fP 404 – 407
f[1] f[1]. wP 408 – 411
f[1]. fP 412 – 415
f[2] f[2]. wP 416 – 419
f[2]. fP 420 – 423
g 424 – 427 10100
g.wP 10100 – 10103 6
g.fP 10104 – 10107 1
g = (struct fraction *)malloc( sizeof(struct fraction));
malloc allocated structure size (8 bytes) of memory for use
malloc( sizeof(struct fraction))
– g (as a pointer variable)
holds the address of this memory location
– g indirection allows the use of the memory
SO: as stated:
typecasting (struct fraction *) allows the O/S
to know how to deal with this memory allocation
47
Structures in C
END OF PART 4
45 46
47 48
13
CS 2211
Systems Programming
Part Ten: Unions
49
Address Label Label Address Value
MEMORY MAPPING – UNIONS
397 – 398 – 399 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
400 – 401 – 402 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
403 – 404 – 405 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
406 – 407 – 408 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
409 – 410 – 411 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
412 – 413 – 414 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
415 – 416 – 417 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
418 – 419 – 420 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
421 – 422 – 423 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
Address Label Label Address Value
MEMORY MAPPING – UNIONS
397 – 398 – 399 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
400 – 401 – 402 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
403 – 404 – 405 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
406 – 407 – 408 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
409 – 410 – 411 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
412 – 413 – 414 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
415 – 416 – 417 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
418 – 419 – 420 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
421 – 422 – 423 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
/* “demographics” is name for union type */
union demographics {
int age /* 1st field is person’s age */
float salary; /* 2nd field is monthly pay */
double emplevel; /* 3rd field is rating scale */
char Owing; /* 4th field is office wing */
}; /* ending ; */
/* means end of union type definition */
– a construct to group together dissimilar
variables under one name
Address Label Label Address Value
MEMORY MAPPING – UNIONS
397 – 398 – 399 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
400 – 401 – 402 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
403 – 404 – 405 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
406 – 407 – 408 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
409 – 410 – 411 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
412 – 413 – 414 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
415 – 416 – 417 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
418 – 419 – 420 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
421 – 422 – 423 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
/* “demographics” is name for union type */
union demographics {
int age /* 1st field is person’s age */
float salary; /* 2nd field is monthly pay */
double emplevel; /* 3rd field is rating scale */
char Owing; /* 4th field is office wing */
}; /* ending ; */
/* means end of union type definition */
– a construct to group together dissimilar
variables under one name
NOTE:
This code does NOT create a variable
(similar to Class declaration in OOP)
– grandfather of this concept
– this is ONLY a template for a variable declaration.
– this creates a USER DEFINED variable type
49 50
51 52
14
Address Label Label Address Value
MEMORY MAPPING – UNIONS
397 – 398 – 399 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
400 – 401 – 402 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
403 – 404 – 405 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
406 – 407 – 408 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
409 – 410 – 411 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
412 – 413 – 414 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
415 – 416 – 417 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
418 – 419 – 420 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
421 – 422 – 423 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
/* “demographics” is name for union type */
union demographics {
int age /* 1st field is person’s age */
float salary; /* 2nd field is monthly pay */
double emplevel; /* 3rd field is rating scale */
char Owing; /* 4th field is office wing */
}; /* ending ; */
/* means end of union type definition */
int i;
union demographics employee;
– a construct to group together dissimilar
variables under one name
creates an integer variable (space to store an integer) i
– and –
creates a demographic variable (space to store ONLY the largest of the variables
in the UNION [in this case 8 bytes] ) employee.
MEMORY MAPPING – UNIONS
Address Label Label Address Value
/* “demographics” is name for union type */
union demographics {
int age /* 1st field is person’s age */
float salary; /* 2nd field is monthly pay */
double emplevel; /* 3rd field is rating scale */
char Owing; /* 4th field is office wing */
}; /* ending ; */
/* means end of union type definition */
/* “demographics” is name for union type */
union demographics {
int age /* 1st field is person’s age */
float salary; /* 2nd field is monthly pay */
double emplevel; /* 3rd field is rating scale */
char Owing; /* 4th field is office wing */
}; /* ending ; */
/* means end of union type definition */
union demographics employee;
MEMORY MAPPING – UNIONS
Address Label Label Address Value
employee employee.age 400 – 403
employee.salary 400 – 403
employee.emplevel 400 – 407
employee.Owing 400
– memory 400 to 407
– the union employee
takes up (requires)
8 bytes of data
note:
employee
employee.salary
employee.age
employee.emplevel
employee.Owing
all returns the address 400
/* “demographics” is name for union type */
union demographics {
int age /* 1st field is person’s age */
float salary; /* 2nd field is monthly pay */
double emplevel; /* 3rd field is rating scale */
char Owing; /* 4th field is office wing */
}; /* ending ; */
/* means end of union type definition */
union demographics employee;
employee.age = 24;
MEMORY MAPPING – UNIONS
Address Label Label Address Value
employee employee.age 400 – 403 24
employee.salary 400 – 403
employee.emplevel 400 – 407
employee.Owing 400
53 54
55 56
15
/* “demographics” is name for union type */
union demographics {
int age /* 1st field is person’s age */
float salary; /* 2nd field is monthly pay */
double emplevel; /* 3rd field is rating scale */
char Owing; /* 4th field is office wing */
}; /* ending ; */
/* means end of union type definition */
union demographics employee;
employee.age = 24;
employee.Owing = ‘D’;
MEMORY MAPPING – UNIONS
Address Label Label Address Value
employee employee.age 400 – 403
employee.salary 400 – 403
employee.emplevel 400 – 407
employee.Owing 400 D
/* “demographics” is name for union type */
union demographics {
int age /* 1st field is person’s age */
float salary; /* 2nd field is monthly pay */
double emplevel; /* 3rd field is rating scale */
char Owing; /* 4th field is office wing */
}; /* ending ; */
/* means end of union type definition */
union demographics employee;
employee.age = 24;
employee.Owing = ‘D’;
MEMORY MAPPING – UNIONS
Address Label Label Address Value
employee employee.age 400 – 403
employee.salary 400 – 403
employee.emplevel 400 – 407
employee.Owing 400 D
CAUTION !
employee.Owing = ‘D’;
assigned the sequence 0100 0100 to the memory location 400
BUT !!! Address 401 and 402 and 403 are UNCHANGED
1.) these held the values from the previous assignment of:
employee.age = 24;
So the sequence that was:
0000 0000 0000 0000 0000 0000 0001 1000
Is now changed to
0100 0100 0000 0000 0000 0000 0001 1000
Now:
printf(“age = %d”,employee.age);
produces:
age = 570425356
MEMORY MAPPING – UNIONS
Address Label Label Address Value
employee employee.age 400 – 403
employee.salary 400 – 403
employee.emplevel 400 – 407
employee.Owing 400 D
/* “demographics” is name for union type */
union demographics {
int age /* 1st field is person’s age */
float salary; /* 2nd field is monthly pay */
double emplevel; /* 3rd field is rating scale */
char Owing; /* 4th field is office wing */
} employee; /* ending ; */
/* means end of union type definition */
union demographics employee;
MEMORY MAPPING – UNIONS
Address Label Label Address Value
employee employee.age 400 – 403
employee.salary 400 – 403
employee.emplevel 400 – 407
employee.Owing 400 D
/* “demographics” is name for union type */
typedef union demographics {
int age /* 1st field is person’s age */
float salary; /* 2nd field is monthly pay */
double emplevel; /* 3rd field is rating scale */
char Owing; /* 4th field is office wing */
} dGraphics; /* ending ; */
/* means end of union type definition */
dGraphics employee;
union demographics employee;
57 58
59 60
16
MEMORY MAPPING – STRUCTURE vs UNION
62
Unions in C
END OF PART 1
61 62