Vous êtes sur la page 1sur 8

ESc101: Fundamentals of Computing

2011-12-Monsoon Semester Lecture #22, September 22, 2011

Please switch off your mobile phones.

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

Karan Singh (E13) has got 60 / 60 marks in mid-sem.

Lec-22

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

Recap
Pointer Operations p Passing arrays as pointers Passing pointers as arrays Sizeof

Lec-22

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

Recap: Passing arrays as pointers


#include <stdio.h> void read_array (int *p, int size) { int i = 0; while (i < size) { scanf (%d, p); p++; i++; } } void print_array (int *p, int size) { int i = 0; while (i < size)
Lec-22

{ printf (%d\t, *p); p++; i++; } printf (\n); } int main ( ) { int a [ ] = {3, -2, 7, 19}; print_array (a, 4); read_array ( , 4); y (a, ); print_array (a, 4); }

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

Recap: Passing pointers as arrays


#include <stdio.h> void read_pointer (int a[ ], int size) { int i = 0; while (i < size) { scanf (%d, &a[i]); i++; } } void print_pointer (int a[ ], int size) { int i = 0; while (i < size)
Lec-22

{ printf (%d\t, a[i]); i++; } printf (\n); } int main ( ) { int b [ ] = {3, -2, 7, 19}; int *p = b; p print_p pointer (p, 4); ); print_pointer (p+1, 3); read_pointer (p, 4); print_pointer (p, 4); }
4

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

Recap: Sizes of variables


Size of a variable can be found out using sizeof function
int *p; a[10]; printf (%d\n, sizeof (int)); printf (%d\n, sizeof (char)); printf (%d\n, sizeof (float)); printf (%d\n, sizeof (double)); printf (%d\n, sizeof (long int)); i f (%d\ i f (l i )) printf (%d\n, sizeof (p)); printf (%d\n, sizeof (a));

Lec-22

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

String copy using arrays


void strcpy (char s [ ], char t [ ]) py ( ] { int i = 0; while (t[i] != \0) { s[i] = t[i]; // copy i++; // point to next element } s[i] = \0; // terminate s }

Lec-22

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

String copy using pointers


void strcpy (char *s, char *t) py ( ) { while (*t != \0) { *s = *t; s++; t++; } *s = \0; }

// copy // point to next element // point to next element // terminate s

Lec-22

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

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-22 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 8

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-22

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

Void Pointers
Useful as a generic pointer type We can use type casting to cast into any type of pointer. One can write functions in a way that different types of arguments can be given to the function as arguments. A pointer to char, float, int, etc., can match as argument against a void pointer type.

Lec-22

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

10

Void Pointers
void generic (void *a, int b) { if (b == 1) { printf (Received Integer Pointer\n); printf (%d\n, * (int *) a); } else if (b == 2) { printf (Received Character Pointer\n); printf (%c\n, * (char *) a); } else if (b == 3) { printf (Received Floating Point Pointer\n); printf (%f\n, * (float *) a); } }
Lec-22 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 11

Void Pointers
#include <stdio.h> // Code for generic (void *a, int b) will come here int main ( ) { int i = 15; char c = y; float f = 15.5; ; generic (&i, 1); generic (&c, 2); generic (&f, 3); }
Lec-22 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 12

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-22 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 13

Any Questions?

Lec-22

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

14

Vous aimerez peut-être aussi