Esc101: Fundamentals of Computing Esc101: Fundamentals of Computing
Esc101: Fundamentals of Computing Esc101: Fundamentals of Computing
Esc101: Fundamentals of Computing Esc101: Fundamentals of Computing
Announcements
Last date for course drop is 20th October.
I will sign drop requests till 17th October.
Lec-23
Lec-23
Recap
Pointer as return type of a function yp Protecting Pointer arguments Void pointer
Lec-23
void f (const int * p) { int j; *p = 0; // Wrong p = &j; // Permitted } void g (int * const p) { int j; *p = 0; // Permitted p = &j; // Wrong }
Lec-23
Multi-dimensional arrays
A single dimensional array
int b [5]; is stored in the following way:
b[0] 1 b[1] 5 b[2] 9 b[3] 13 b[4] 17
A 2-dimensional array
int a [4][3]; is stored in the following way:
a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2] a[2][0] a[2][1] a[2][2] a[3][0] a[3][1]
1 a[0]
Lec-23
13 a[1]
17
21
25 a[2]
29
33
37 a[3]
41
7
Strings
char *str = Kanpur; str Kanpur ; printf (%s\n, s);
Compiler will allocate sufficient space for the string Kanpur in this case. The address of the first character of the string will be assigned to variable str. str
Lec-23
Arrays of Pointers
Arrays of pointers can be declared For example: char *a[3]; declares a to be an array of 3 pointers to char a[i] is a pointer to char Common way to declare arrays of strings (dont do this in this weeks lab assignments) Very useful since the strings can be of variable size char * a[3] = { Kanpur, Kanpur Delhi, This is a long name };
Lec-23 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 10
Arrays of Pointers
If we do not know what to initialize the string as
how much memory would be allocated by compiler for each string
Actually, none. It requires dynamic memory allocation.
char *a[3]; would allocate just enough space to store three addresses and addresses, no space for the actual string.
Lec-23
11
If space cannot be allocated, null pointer is returned Space should be freed after use using free It takes a pointer allocated using malloc as a parameter, free (a);
Lec-23 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 12
Dynamic Array
#include <stdio.h> #include <stdlib.h> // required for malloc int main ( ) { double *a; int i, n; printf (Enter the size of array: ); scanf (%d, &n); a = (double *) malloc (n * sizeof (double)); // size of (double) is required as it is in bytes for (i = 0; i < n; i++) ( ; ; ) a [i] = i; // array notation free (a); // important to free space }
Lec-23 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 13
Dynamic Array
#include <stdio.h> #include <stdlib.h> // required for malloc int main ( ) { char *str[3]; int i, n[3]; printf (Enter the maximum sizes of three strings: ); scanf (%d %d %d, &n[0], &n[1], &n[2]); for (i = 0; i < 3; i++) str[i] = (char *) malloc ((1 + n[i] ) * sizeof (char)); strcpy (str[0], Kanpur); strcpy (str[1], New Delhi); py ( [ ], p ); py ( [ ], ); for (i = 0; i < 3; i++) free (str[i]); // important to free space }
Lec-23 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 14
Files
Files are used for getting input and storing output A file is accessed using a pointer to a file: FILE *fp; To read or write a file, it must be first opened using fopen fopen returns a file pointer Takes two strings as parameters
First one is the name of the file Second one is the mode of operation r for reading: error if file does not exist w for writing: file created if does not exist, overwritten if exists w a for appending: file created if does not exists, contents preserved, if it exists
getc and putc analogous to getchar and putchar require an extra file pointer argument
char c = getc (fp); g putc (c, fp);
feof is used to check if the file is finished (while reading) while (!feof (fp))
Lec-23
16
Any Questions?
Lec-23
17