This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
1
6.1 Basics of Structures
6.2 Structures and Functions
6.3 Arrays of Structures
6.4 Pointers to Structures
6.5 Self-referential Structures + malloc + linked list
(6.6 Table Lookup –– not treated in this course)
6.7 Typedef
6.8 Unions
6.9 Bit-fields
Chap. 6 Structures + malloc + linked list
Imperative and System Programming, B. Hirsbrunner, diuf.unifr.ch/pai/ip, Lecture 7 – 7 November 2012KR6 + malloc + linked list: ~90’; Exercises: ~90’
2
6.1 Basics of Structures: definition and examples
Definition
A structure is a collection of one or more variables, possibly of different types.
Examples of type declarations
(no memory allocation)
struct {int x; int y};
struct point {int x; int y;};
– ‘point' is an optional structure tag
Examples of variables definitions
(with memory allocation)
struct {int x; int y} p;
struct point {int x; int y} p1;
struct point p2;
Examples of variables definitions with initialization
struct {int x; int y} p = {5, 5};
struct point {int x; int y} p1 = {0,1};
struct point p2 = {1,2};
3
6.1 Basics of Structures: more definitions and examples
Definitions
1. The variables named in a structure are called members.
2. A struct declaration 'struct {…};' defines a type
i.e. 'struct {…} x;' is syntactically analogous to 'int x;'
More examples of variable declarations/definitions with initialization
1. struct rectangle { struct point p1; struct point p2} r = {{0,1}, {1,2}} ;
// nested structures with initialization
1. struct point f(…); // a function returning a value of type 'struct point'
1. { // local 'struct point' variable definitions with initialization
struct point p1 = {0,1}, p2 = f(…); // assuming that f(…) returns a value of type struct point
}
4
6.1 Basics of Structures: name overloading
Name OverloadingFollowing objects can have the same name without conflicting, since they can
always be distinguished by context:- structure tag - structure member variable- ordinary (i.e. non-member) variable
Example (overloading should be used very carefully)
struct x {int x;}; // declaraton of type 'struct x', with a member x
struct x x = {1}; // declaration of variable x of type 'struct x',
// with member x initialized to 1
Usage: printf("%d", x.x);
5
6.1 Basics of Structures: structure member operator '.'
Definition
A member of a particular structure is referred as follows: structure_name.member
Examples of expressions with the structure member operator '.'
p.x
r.pt1.x
Memory Diagrams
6
6.2 Structures and Functions
Legal Operations
- assignment
- passing arguments to functions and returning values from functions
- taking its address with &
- accessing its members with . or –>
- initialization by a list of constant member values
Parameter Passing to Functions
- pass components separately, eventually pass their addresses
- pass an entire structure, eventually its address
Remark
Structure parameter are passed by value like any others
7
6.2 Structures and Functions : example 1
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
/* canonrect: canonicalize coordinates of rectangle */
struct rect canonrect(struct rect r)
{
struct rect temp;
temp.p1.x = min(r.p1.x, r.p2.x);
temp.p1.y = min(r.p1.y, r.p2.y);
temp.p2.x = max(r.p1.x, r.p2.x);
temp.p2.y = max(r.p1.y, r.p2.y);
return temp;
}
8
6.2 Structures and Functions : example 2
struct rect canonrect(struct rect *rp)
{
struct rect temp;
temp.p1.x = min((*rp).p1.x, (*rp).p2.x);
temp.p1.y = min((*rp).p1.y, (*rp).p2.y);
temp.p2.x = max( rp->p1.x, rp->p2.x);
temp.p2.y = max( rp->p1.y, rp->p2.y);
return temp;
}
Remark. Alternative notation for pointer p to a structure:
p –> member-of-structure
9
6.2 Structures and Functions : . and –>
Both . and -> associate from left to right, so if we have
struct rect r, *rp = &r;
then these six expressions are equivalent:
r.p1.x
(r.p1).x
(*rp).p1.x
((*rp).p1).x
rp->p1.x
(rp->p1).x
Remark. The structure operators . and ->, together with () for function calls and [] for subscripts, are at the top of the precedence hierarchy and thus bind very tightly.
10
6.3 Arrays of Structures: example + size of
struct key {
char *word;
int count;
} keytab[NKEYS] = {{"auto",0}, ...};
sizeof (unary operator used to calculate the amount of space any data-element / data-types occupies in memory, in number of bytes) :
sizeof variable or expression // can also be specified with parentheses
()sizeof (type-name) // parentheses () are mandatory
- declares a structure type 'struct key', - defines an array 'keytab[NKEYS]' this type, and - sets aside storage for them
6.4 Pointers to Structures6.5 Self-referential Structures: declarations and definitions
struct node_t {int data; struct node_t *next};
struct node_t node = {1, &node};
struct node_t n2 = {2, NULL}, n1 = {1, &n2};
struct s {char data; struct t *sp} S;
struct t {int data; struct s *tp} T;
T.tp = &S; S.sp = &T;
12
malloc, realloc, free
#include <stdlib.h>
void *malloc(size_t size);
void *realloc(void *ptr, size_t size)
void free(void *ptr);
malloc: allocates a memory of size bytes, and returns a pointer to the first element (a NULL pointer if the memory couldn't be allocated)
Exampleint *ptr;
ptr = malloc(sizeof(int));
ptr = realloc(ptr, sizeof(int));
free(ptr);
realloc: changes the size of the object pointed to by ptr to size. The content will be unchanged up to the minimum of the old and new sizes. It returns a pointer with the same meaning as the one returned by malloc.
free: deallocates the memory pointed by ptr, and previously allocated by malloc or realloc
13
6.5 Self-referential Structures: push/pop example
(1) struct node_t { int data; struct node_t *previous;}; // node type of a stack
(2) struct node_t *stack_ptr = NULL; // 'stack_ptr' variable points to the top node;