structures. heterogeneous structures collection of values of possibly different types. name the...
TRANSCRIPT
![Page 1: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/1.jpg)
Structures
![Page 2: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/2.jpg)
Heterogeneous Structures
Collection of values of possibly different types.
Name the collection.Name the components.
Example : Student recordSinghal
name "V Singhal"rollno "00CS1001"classtest 14midterm 78final 73grade ‘B
![Page 3: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/3.jpg)
Structure : terminology A struct is a group of items (variables)
which may be of different types. Each item is identified by its own
identifier, each of which is known as a member or field of the structure.
A struct is sometimes called a record or structure.
Structs are the basis of classes in C++ and Java.
![Page 4: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/4.jpg)
Structure declaration
struct {char first[10];char midinit;char last[20];
} sname, ename;
This declaration createstwo structure variables,sname and ename, eachof which contains 3members.We can use sname.first,ename.midinit, etc.
![Page 5: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/5.jpg)
Members To access the members of a
structure, we use the member access operator “.”.strcpy (sname.first, “Sudeshna”);sname.midinit = ‘K’;strcpy (sname.last, “Sarkar”) ;
![Page 6: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/6.jpg)
Tagged structure
struct nametype {char first[10];char midinit;char last[20];
};struct nametype sname, ename;typedef struct nametype
NTYPE;NTYPE aname, bname;
This definition createsa structure tag nametypecontaining 3 members:first, midinit, last.Variables may be declaredof type struct <tagname>.
typedef is normally usedto give names to a struct type.
![Page 7: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/7.jpg)
typedeftypedef struct {
char first[10];char midinit;char last[20];
} NAMETYPE;NAMETYPE
sname,ename;
![Page 8: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/8.jpg)
Another example#define MAX_NAME 40typedef struct {
char name[MAX_NAME+1];char rollno[10];int classtest;int midterm;int final;char grade;
} StudentRecord;Defines a new data type called StudentRecord.
Does not declare a variable.
![Page 9: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/9.jpg)
Declaring struct variables
/* typedef structs go at top of program */. . .int .....float ....StudentRecord s1;StudentRecord singhal ;/* StudentRecord is a type; s1 and singhal are variables*/
struct nametype aname;/* struct nametype is a type; aname is a variable */
![Page 10: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/10.jpg)
Things you can and can't do
You canUse = to assign whole struct variables
You can Have a struct as a function return type
You cannot Use == to directly compare struct
variables; can compare fields directly You cannot
Directly scanf or printf structs; can read fields one by one.
![Page 11: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/11.jpg)
Struct initializers
/* typedef structs go on top */StudentRecord s1 = {"V Singhal",
"00CS1002", 15, 78, 73, 'B'};
Using components of struct variables
s1.classtest = 46;s1.midterm = 78;scanf ("%d", &s1.rollno) ;
![Page 12: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/12.jpg)
Assigning whole structs
s1 = singhal;is equivalent to
strcpy(s1.name, singhal.name) ; strcpy(s1.rollno, singhal.rollno; s1.classtest = singhal.classtest; s1.midterm = singhal.midterm; s1.final = singhal.final; s1.grade = singhal.grade;
![Page 13: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/13.jpg)
Within a given structure, the member names must be unique.
However, members in different structures may have the same name.
A member is always accessed through a structure identifier.struct fruit {
char name[20];int calories;
};struct vegetable { char name[30];
int calories;};
struct fruit mango;struct vegetable potato;It is clear that we can access mango.calories andpotato.calories withoutany ambiguity.
![Page 14: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/14.jpg)
Complicated structures A member of a structure can be an array or
another structure.struct grocerylist {
struct fruit flist[10];struct vegetable vlist[20];
} ; You can have an array of structures.
struct card {int pips;char suit;
} deck[52] ;
![Page 15: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/15.jpg)
A function using struct array
int fail (StudentRecord slist []) {int i, cnt=0;for (i=0; i<CLASS_SIZE; i++)
cnt += slist[i].grade == ‘F’;return cnt;
}
![Page 16: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/16.jpg)
Using structures with functions Structures can be passed as arguments to
functions. Structures can be returned from functions. Call by value is used if a structure is a function
parameter, meaning that a local copy is made for use in the body of the function. If a member of the structure is an array, then the array gets copied as well.
If the structure is large, passing the structure as an argument can be relatively inefficient. An address of th structure may be used as the parameter.
![Page 17: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/17.jpg)
Union A union is like a structure, except that the
members of a union share the same space in memory.union int_or_float {
int i;float f;
}; It is the programmer’s responsibility to know
which representation is currently stored in a union variable.
![Page 18: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/18.jpg)
Arrays of Structures A struct represents a single record. Typically structs are used to deal with
collections of such records Examples : student records, employee
records, book records, ... In each case we will hav multiple instances
of the struct type.Arrays of structs are the natural way to do
this.
![Page 19: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/19.jpg)
Arrays of structs : declaration & use
Each declaration below declares an array, where each array element is a structure:point corner_points[10] ;StudentRecord btech01[MAXS] ;
We access a field of a struct in an array by specifying the array element and then the field :btech01[i].namecorner_points[4].x
![Page 20: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/20.jpg)
Naming in struct Arrayspoint pentagon[5];
xy
xy
xy
xy
xy
pentagon : an array of points
pentagon[1] : a point structure
pentagon[4].x : a double
![Page 21: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/21.jpg)
Using Arrays of structs
StudentRecord class[MAXS];...for (i=0; i<nstudents; i++) {
scanf (“%d%d”, &class[i].midterm, &class[i].final);
class[i].grade = (double)(class[i].midterm+class[i].final)/50.0;
}
![Page 22: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/22.jpg)
struct Array elements as parameters
void draw_line (point p1, point p2) { ... }...point pentagon[5];...for (i=0;i<4;i++)
draw_line (pentagon[i], pentagon[i+1]);
draw_line (pentagon[4], pentagon[0]);
![Page 23: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/23.jpg)
structs as Parameters
A single struct is passed by value. all of its components are copied from
the argument (actual parameter) to initialize the (formal) parameter.
point set_midpt (point a, point b) { ... }int main (void) {
point p1, p2, m;...m = set_midpt(p1, p2);
}
![Page 24: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/24.jpg)
Passing Arrays of structs An array of structs is an array. When any array is an argument (actual parameter), it
is passed by reference, not copied [As for any array] The parameter is an alias of the actual array
argument.int avg (StudentRec class[MAX]) { ... }int main (void) {
StudentRec bt01[MAX];int average;...average = avg_midpt(bt01) ;
}
![Page 25: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/25.jpg)
Dynamic Memory Allocation,Structure pointers
![Page 26: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/26.jpg)
Basic Idea Many a time we face situations where
data is dynamic in nature. Amount of data cannot be predicted
beforehand. Number of data item keeps changing
during program execution. Such situations can be handled more
easily and effectively using dynamic memory management techniques.
![Page 27: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/27.jpg)
C language requires the number of elements in an array to be specified at compile time. Often leads to wastage or memory
space or program failure. Dynamic Memory Allocation
Memory space required can be specified at the time of execution.
C supports allocating and freeing memory dynamically using library routines.
![Page 28: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/28.jpg)
Memory Allocation Process in C
Local variables
Free memory
Global variables
InstructionsPermanent storage area
Stack
Heap
![Page 29: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/29.jpg)
The program instructions and the global variables are stored in a region known as permanent storage area.
The local variables are stored in another area called stack.
The memory space between these two areas is available for dynamic allocation during execution of the program. This free region is called the heap. The size of the heap keeps changing
![Page 30: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/30.jpg)
Memory Allocation Functions
malloc: Allocates requested number of bytes and returns a pointer to the first byte of the allocated space.
calloc: Allocates space for an array of elements, initializes them to zero and then returns a pointer to the memory.
free : Frees previously allocated space. realloc: Modifies the size of previously
allocated space.
![Page 31: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/31.jpg)
Dynamic Memory Allocation used to dynamically create
space for arrays, structures, etc.
int main () { int *a ; int n; .... a = (int *) calloc (n, sizeof(int)); ....}
a = malloc (n*sizeof(int));
![Page 32: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/32.jpg)
Space that has been dynamically allocated with either calloc() or malloc() does not get returned to the function upon function exit.
The programmer must use free() explicitly to return the space. ptr = malloc (...) ; free (ptr) ;
![Page 33: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/33.jpg)
void read_array (int *a, int n) ;int sum_array (int *a, int n) ;void wrt_array (int *a, int n) ;
int main () { int *a, n; printf (“Input n: “) ; scanf (“%d”, &n) ; a = calloc (n, sizeof (int)) ; read_array (a, n) ; wrt_array (a, n) ; printf (“Sum = %d\n”, sum_array(a, n);}
![Page 34: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/34.jpg)
void read_array (int *a, int n) { int i; for (i=0; i<n; i++)
scanf (“%d”, &a[i]) ;}void sum_array (int *a, int n) { int i, sum=0; for (i=0; i<n; i++) sum += a[i] ; return sum;}void wrt_array (int *a, int n) { int i; ........}
![Page 35: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/35.jpg)
Arrays of Pointers Array elements can be of any type
array of structures array of pointers
![Page 36: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/36.jpg)
int main (void) {char word[MAXWORD];char * w[N]; /* an array of pointers */int i, n; /* n: no of words to sort */
for (i=0; scanf(“%s”, word) == 1); ++i) { w[i] = calloc (strlen(word)+1, sizeof(char)); if (w[i] == NULL) exit(0);
strcpy (w[i], word) ;}
n = i;sortwords (w, n) ;wrt_words (w, n);return 0;
}
![Page 37: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/37.jpg)
w
0
1
2
3
17
Input : A is for apple or alphabet pie which all get a slice of come taste it and try
A \0
i s \0
f o r \0
a p p l e \0
t r y \0
![Page 38: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/38.jpg)
void sort_words (char *w[], int n) { int i, j; for (i=0; i<n; ++i)
for (j=i+1; j<n; ++j)if (strcmp(w[i], w[j]) > 0) swap (&w[i], &w[j]) ;
}void swap (char **p, char **q) { char *tmp ; tmp = *p; *p = *q; *q = tmp;}
![Page 39: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/39.jpg)
w
w[i]
f o r \0
a p p l e \0
Before swapping
w[j]
![Page 40: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/40.jpg)
w
w[i]
f o r \0
a p p l e \0
After swapping
w[j]
![Page 41: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/41.jpg)
Pointers to Structure
![Page 42: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/42.jpg)
Pointers and Structures You may recall that the name of an array
stands for the address of its zero-th element. Also true for the names of arrays of structure
variables. Consider the declaration:
struct stud { int roll; char dept_code[25]; float cgpa; } class[100], *ptr ;
![Page 43: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/43.jpg)
The name class represents the address of the zero-th element of the structure array.
ptr is a pointer to data objects of the type struct stud.
The assignmentptr = class ;
will assign the address of class[0] to ptr. When the pointer ptr is incremented by
one (ptr++) : The value of ptr is actually increased
by sizeof(stud). It is made to point to the next record.
![Page 44: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/44.jpg)
Once ptr points to a structure variable, the members can be accessed as: ptr –> roll ; ptr –> dept_code ; ptr –> cgpa ;
The symbol “–>” is called the arrow operator.
![Page 45: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/45.jpg)
Warning When using structure pointers, we should take
care of operator precedence. Member operator “.” has higher precedence than “*”.
ptr –> roll and (*ptr).roll mean the same thing. *ptr.roll will lead to error.
The operator “–>” enjoys the highest priority among operators.
++ptr –> roll will increment roll, not ptr. (++ptr) –> roll will do the intended thing.
![Page 46: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/46.jpg)
Program to add two complex numbers using pointers
typedef struct { float re; float im;} complex;main() { complex a, b, c; scanf (“%f %f”, &a.re, &a.im); scanf (“%f %f”, &b.re, &b.im); add (&a, &b, &c) ; printf (“\n %f %f”, c,re, c.im);}
![Page 47: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/47.jpg)
void add (complex * x, complex * y, complex * t) {
t->re = x->re + y->re ;
t->im = x->im + y->im ;
}
![Page 48: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/48.jpg)
Structure and list processing
![Page 49: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/49.jpg)
Dynamic allocation: review Variables in C are allocated in one of 3
spots: the run-time stack : variables declared local to
functions are allocated during execution the global data section : Global variables are
allocated here and are accessible by all parts of a program.
the heap : Dynamically allocated data items malloc, calloc, realloc manage the heap region of
the mmory. If the allocation is not successful a NULL value is returned.
![Page 50: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/50.jpg)
Bad Pointers When a pointer is first allocated, it does not
have a pointee. The pointer is uninitialized or bad. A dereference operation on a bad pointer is a
serious runtime error. Each pointer must be assigned a pointee before
it can support dereference operations. int * numPtr;
Every pointer starts out with a bad value. Correct code overwrites the bad value.
![Page 51: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/51.jpg)
Example pointer code.
int * numPtr;int num = 42;numPtr = #*numPtr = 73;numPtr = malloc (sizeof (int));*numPtr = 73;
![Page 52: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/52.jpg)
int a=1, b=2, c=3;
int *p, *q;1a
3c
2b
xxx
xxx
p
q
![Page 53: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/53.jpg)
p = &a ;q = &b ;
1a
3c
2b
p
q
![Page 54: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/54.jpg)
c = *p ;p = q ;*p = 13 ;
1a
1c
13b
p
q
![Page 55: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/55.jpg)
Bad pointer Examplevoid BadPointer () {
int *p;*p = 42;
}int * Bad2 () {
int num, *p;num = 42;p = #return p;
}
x x xp
X
![Page 56: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/56.jpg)
A function call malloc(size) allocates a block of mrmory in the heap and returns a pointer to the new block. size is the integer size of the block in bytes. Heap memory is not deallocated when the creating function exits.
malloc generates a generic pointer to a generic data item (void *) or NULL if it cannot fulfill the request.
Type cast the pointer returned by malloc to the type of variable we are assigning it to.
free : takes as its parameter a pointer to an allocated region and de-allocates memory space.
![Page 57: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/57.jpg)
Dynamic memory allocation: review
typedef struct {int hiTemp;int loTemp;double precip;
} WeatherData;main () {
int numdays;WeatherData * days;scanf (“%d”, &numdays) ;days=(WeatherData *)malloc (sizeof(WeatherData)*numdays);if (days == NULL) printf (“Insufficient memory”);...
free (days) ;}
![Page 58: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/58.jpg)
Self-referential structures Dynamic data structures : Structures
with pointer members that refer to the same structure. Arrays and other simple variables are
allocated at block entry. But dynamic data structures require
storage management routine to explicitly obtain and release memory.
![Page 59: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/59.jpg)
Self-referential structures
struct list {int data ;struct list * next ;
} ;
The pointer variable next is called a link.Each structure is linked to a succeeding structureby next.
![Page 60: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/60.jpg)
Pictorial representationA structure of type struct list
data next
The pointer variable next contains either • an address of the location in memory of the successor list element• or the special value NULL defined as 0.
NULL is used to denote the end of the list.
![Page 61: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/61.jpg)
struct list a, b, c;a.data = 1;b.data = 2;c.data = 3;a.next = b.next = c.next = NULL;
1 NULL
data next
a2 NULL
data next
b3 NULL
data next
c
![Page 62: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/62.jpg)
Chaining these togethera.next = &b;b.next = &c;
1
data next
a2
data next
b3
data next
cNULL
What are the values of : • a.next->data• a.next->next->data
23
![Page 63: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/63.jpg)
Linear Linked Lists A head pointer addresses the first
element of the list. Each element points at a successor
element. The last element has a link value
NULL.
![Page 64: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/64.jpg)
Header file : list.h#include <stdio.h>#include <stdlib.h>typedef char DATA;struct list {
DATA d;struct list * next;
};typedef struct list ELEMENT;typedef ELEMENT * LINK;
![Page 65: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/65.jpg)
Storage allocationLINK head ;
head = malloc (sizeof(ELEMENT));
head->d = ‘n’;
head->next = NULL;
creates a single element list.
n NULLhead
![Page 66: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/66.jpg)
Storage allocationhead->next = malloc (sizeof(ELEMENT));head->next->d = ‘e’;head->next->next = NULL;
A second element is added.
nhead e NULL
![Page 67: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/67.jpg)
Storage allocationhead->next=>next = malloc
(sizeof(ELEMENT));head->next->next->d = ‘e’;head->next->next-> = NULL;
We have a 3 element list pointed to by head.The list ends when next has the sentinel value NULL.
nhead e w NULL
![Page 68: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/68.jpg)
List operations Create a list Count the elements Look up an element Concatenate two lists Insert an element Delete an element
![Page 69: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/69.jpg)
Produce a list from a string (recursive version)#include “list.h”LINK StrToList (char s[]) {
LINK head ;if (s[0] == ‘\0’)
return NULL ;else {
head = malloc (sizeof(ELEMENT));head->d = s[0];head->next = StrToList (s+1);return head;
}}
![Page 70: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/70.jpg)
#include “list.h”LINK SToL (char s[]) {
LINK head = NULL, tail;int i;if (s[0] != ‘\0’) {
head = malloc (sizeof(ELEMENT));head->d = s[0];tail = head;for (i=1; s[i] != ‘\0’; i++) { tail->next = malloc(sizeof(ELEMENT)); tail = tail->next; tail->d = s[i];}tail->next = NULL;
}return head;
}
list from a string (iterative version)
![Page 71: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/71.jpg)
?Ahead
tail
1. A one-element list
2. A second element is attached
Ahead
tail
??
3. Updating the tail
Ahead
tail
?B
4. after assigning NULL
Ahead
tail
NULLB
![Page 72: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/72.jpg)
/* Count a list recursively */int count (LINK head) {
if (head == NULL)return 0;
return 1+count(head->next);}
/* Count a list iteratively */int count (LINK head) {
int cnt = 0;for ( ; head != NULL; head=head->next)
++cnt;return cnt;
}
![Page 73: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/73.jpg)
/* Print a List */void PrintList (LINK head) {
if (head == NULL)printf (“NULL”) ;
else {printf (“%c --> “, head->d) ;PrintList (head->next);
}}
![Page 74: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/74.jpg)
/* Concatenate two Lists */
void concatenate (LINK ahead, LINK bhead) {
if (ahead->next == NULL)ahead->next = bhead ;
elseconcatenate (ahead->next, bhead);
}
![Page 75: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/75.jpg)
Insertion Insertion in a list takes a fixed amount of
time once the position in the list is found.
A C
p2p1
Bq
Before Insertion
![Page 76: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/76.jpg)
Insertion/* Inserting an element in a linked list. */void insert (LINK p1, LINK p2, LINK q) {
p1->next = q;q->next = p2;
}
A C
p2p1
Bq
After Insertion
![Page 77: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/77.jpg)
DeletionBefore deletion
1 2 3
p
p->next = p->next->next;
After deletion
1 2 3
p garbage
![Page 78: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/78.jpg)
DeletionBefore deletion
1 2 3p
q = p->next;p->next = p->next->next;
After deletion
1 2 3
p
qfree (q) ;
![Page 79: Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f2c5503460f94c46d64/html5/thumbnails/79.jpg)
Delete a list and free memory/* Recursive deletion of a list */void delete_list (LINK head) {
if (head != NULL) {delete_list (head->next) ;free (head) ; /* Release
storage */}