Você está na página 1de 9

ESc101: Fundamentals of Computing

2011-12-Monsoon Semester Lecture #23, September 26, 2011

Please switch off your mobile phones.

Announcements
Last date for course drop is 20th October.
I will sign drop requests till 17th October.

Lec-23

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Best Programmers in Lab 5


Section E1 E3 E5 E7 E9 E11 E13 E15 Name Deepak Choudhary Rahul Jain Tanvi Soni Ishendra Agarwal Atul Agarwal Varun Harbola Karan Singh Abhinay Kumar Section E2 E4 E6 E8 E10 E12 E14 --Sanjay Moudgalya Prashant Dubey Prithvi Sharma Shreshth Gandhi Aashish Gupta Name Rohit Prakash Barnwal

Lec-23

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Recap
Pointer as return type of a function yp Protecting Pointer arguments Void pointer

Lec-23

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Recap: Pointer as Return Type


int * max (int *a, int *b) { if (*a > *b) return a; else return b; } int main ( ) { int *p, i, j; p = max (&i, &j); }
Lec-23 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 4

Recap: Protecting Pointer Arguments


Pointer arguments can be protected against accidental changes by putting qualifier const. void h (const int * const p) { int j; p // Wrong g *p = 0; p = &j; // Wrong }

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

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Recap: Generic Exchange


void genericexchange (void *a, void *b, int c) { if (c == 1) { int temp = * (int *) a; ( ) ( ) * (int *) a = * (int *) b; * (int *) b = temp; } else if (c == 2) { char temp = * (char *) a; * (char *) a = * (char *) b; * (char *) b = temp; } else if (c == 3) { float temp = * (float *) a; p ( ) ; * (float *) a = * (float *) b; * (float *) b = temp; } }
Lec-23 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 6

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

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Multi-dimensional arrays and pointers


A multi-dimensional array name is again a pointer To access an array element a[i][j], compiler will do the following: a points to 0th row (a + i) points to ith row This address will be computed as a + i * (size of row) a + i * (number of elements in row) * size of (int) For a [4][3], it will be: a + i*3*4 Address of a[i][j] will be: a + i * 12 + j * size of (int) i f (i ) a + 12 * i + 4 * j Since size of row requires number of elements, this number is required when multi-dimensional arrays are passed to functions
Lec-23 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 8

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

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

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

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

11

Dynamic Memory Allocation


Dynamic memory allocation is required when the programmer cannot determine in advance the space required by the program. Putting a large number as the maximum limit works, but it wastes a lot of memory y Space is dynamically allocated using malloc It takes size in bytes as a parameter and returns void *, i.e., a pointer without a specific type So, it requires type casting. For example:
int *a; a = (int *) malloc (5 * sizeof (int)) ; is equivalent to declaring an array of 5 integers, integers int a [5];

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

fp = fopen (input.txt, r);


After finishing operations, file must be closed using, fclose (fp);
Lec-23 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 15

Input/Output using files


fscanf and fprintf analogous to scanf and printf same parameters except an extra parameter in the beginning the fi parameter is the file pointer h first i h fil i
fscanf (fp, %d, &n); fprintf (fp, %d, n);

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

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

16

Any Questions?

Lec-23

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

17

Você também pode gostar