Pointers Part 2-Introduction to Computer Programming-Lecture Slides, Slides of Computer Programming

Dr. Mehandi Nandakumar delivered this lecture at Baddi University of Emerging Sciences and Technologies for Introduction to Computer Programming course. Its main points are: Void, Pointer, Pointers, Multidimensional, Arrays, String, Dynamic, Allocation, Memory, Leak

Typology: Slides

2011/2012

Uploaded on 07/13/2012

ekbaal
ekbaal 🇮🇳

3

(1)

30 documents

1 / 18

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 192
Lecture 13
Winter 2003
January 5-6, 2004
Dr. Shafay Shamail
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download Pointers Part 2-Introduction to Computer Programming-Lecture Slides and more Slides Computer Programming in PDF only on Docsity!

CS 192Lecture 13Winter 2003

January 5-6, 2004Dr. Shafay Shamail

Void Pointer

int age = 20;int *ageptr = &age;void *vptr = (void *)ageptr;int *newptr = (int *)vptr;Can’t do:vptr++vptr—vptr + 2As the base type of vptr is not known

Initializing Multi-Dimentional Arrays

static int example[5][3] ={

static int example[5][3] ={

Initializing Multi-Dimensional Arraysstatic int example[][3][2] ={

}; 4-by-3-by-2 array

Arrays of Pointers …

-^ Two dimensional string pointers e.g. C++ dictionary^ char

*keyword[][2]

=

{ "for",

"for(initialization;

condition;

increment)",

"if",

"if(condition)

...^

else^

...",

"switch",

"switch(value)

{^ case-list

}",

"while",

"while(condition)

...",

//^ add

the^

rest^

of^ the

C++^

keywords

here

"",^ ""

//^

terminate

the^

list^

with^

nulls

}; int main(){ char

str[80];int i;cout <<^ "Enter

keyword:

";

cin^ >>

str; for(i=0;

*keyword[i][0];

i++)

if(!strcmp(keyword[i][0],

str))

cout^

<<^ keyword[i][1]; return

0; }

Array of Strings vs. Array of Pointers

^ char

movies[5][20]

{“Godfather”,“Maula

Jatt”, “A^ Fish

Called

Wanda”,

“Blade

Runner”, “Spiderman”}; ^ char

*movies[5]

{“Godfather”,“Maula

Jatt”, “A^ Fish

Called

Wanda”,

“Blade

Runner”, “Spiderman”};

  • each column is 20characters wide due tothe longest moviename• wasted space• the strings are placedcontiguously inmemory withoutwasting space• pointers in the array^ movie

point to them

int xptr=new int;xptr = 73;int x2ptr = new int;x2ptr = 65;

Dynamic Allocation

What is wrong here?int xptr = new int;xptr = 73;int x2ptr;x2ptr=65;

Dynamic Allocation

//What is wrong here?int xptr = new int;xptr

int *x2ptr = new int;x2ptr

= xptr;

*x2ptr

//memory leak

Dynamic Allocation

int *myptr = new int(73);cout << *myptr;delete myptr;

Dynamic Allocation

Array of Pointers

const int RSIZE = 4;const int CSIZE = 6;int * iarray[RSIZE];for(int k=0; k<RSIZE; k++){ iarray[k] = new int [CSIZE];if (iarray[k] == NULL){

cerr << “Memory Allocation Error…\n”;cerr << “Exiting …\n”;exit(-1); } }

A two dimensional

array ofRSIZE*CSIZE

iarray 0 1 2 3

Double and Tripple Pointers

int a = 20;int *ip = &a;int **ipp = &ip;int ***ippp = &ipp;cout << a << endl;cout << ip << *ip << endl;cout << ipp << *ipp << **ipp << endl;cout << ippp << *ippp << **ippp << ***ippp << endl;

20 a 100

100 ip^200

200 ipp^300

300 ippp^400