programming in c++

23
Programming In C++ Spring Semester 2013 Lecture 7 Programming In C++, Lecture 7 By Umer Rana

Upload: isaac-cox

Post on 30-Dec-2015

16 views

Category:

Documents


0 download

DESCRIPTION

Programming In C++. Spring Semester 2013 Lecture 7. Pointers. A variable that holds an address value of the variable is called a pointer variable or simple pointer. Pointer does not have any data type, it only tells the compiler what kind of variable the pointer point to. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Programming In C++

Programming In C++

Spring Semester 2013Lecture 7

Programming In C++, Lecture 7 By Umer Rana

Page 2: Programming In C++

Pointers

Programming In C++, Lecture 7 By Umer Rana

• A variable that holds an address value of the variable is called a pointer variable or simple pointer.

• Pointer does not have any data type, it only tells the compiler what kind of variable the pointer point to.

• A pointer can hold the address of any variable of the correct type.

• A pointer provides a way of accessing a variable without referring to the variable directly.

• The address acts as an intermediary between the variable and the program accessing it.

• Pointer is a variable that represents the location of a data item, such as variable or an array element.

Page 3: Programming In C++

Pointers

Programming In C++, Lecture 7 By Umer Rana

• Suppose v is a variable that represents some particular data item.

• The address of v’s memory location can be determined by the expression &v, where & is the unary operator (a unary operation is an operation with only one operand), called the address operator, that evaluates the address of its operand.e.g. int main(void)

{int v;printf("%d",&v);getch();

}

Page 4: Programming In C++

Pointers Declaration

Programming In C++, Lecture 7 By Umer Rana

• Pointer variables, like all other variables, must be declared before, they may be used in C program.

• When a pointer variable is declared, the variable name must be preceded by an asterisk(*).

• This identifies the fact that the variable is a pointer.

• The data type that appears in the declaration refers to the object of the pointer.

• Thus a pointer declaration may be written in general terms as :

Data-type *ptr;

Page 5: Programming In C++

Pointers Notation

Programming In C++, Lecture 7 By Umer Rana

• Now let us assign the address of v to another

variable pointer type *ptrInt.

int *ptrInt;ptrInt = &v;

• The data item represented by v. can be accessed by the expression *ptrInt.

• Where * is a unary operator called the indication operator, that operates only on a pointer variable.

• Therefore, *ptrInt and v both represent the same data item.

Page 6: Programming In C++

Pointers Show Address

Programming In C++, Lecture 7 By Umer Rana

int main(void){

int day;int *ptrInt;ptrInt=&day;

printf("%d",&day);printf("\n%d",ptrInt);getch();

}

Page 7: Programming In C++

Pointers Show Values

Programming In C++, Lecture 7 By Umer Rana

int main(void){

int day=10;int *ptrInt;ptrInt=&day;

printf("Show Address of the Variable\n");printf("\n%d",&day);printf("\n%d",ptrInt);

printf("\n\nShow value of the Variable\n");printf("\n%d",day);printf("\n%d",*ptrInt);

getch();}

Page 8: Programming In C++

Pointers Initializing

Programming In C++, Lecture 7 By Umer Rana

• Within a variable declaration, a pointer variable can be initialized by assigning in the address of another variable.

• Remember that the variable whose address is assigned to the pointer variable must have been declared earlier in the program, for example.

int i;int *ptrInt=&i;

Page 9: Programming In C++

Pointers Initializing

Programming In C++, Lecture 7 By Umer Rana

int main(void){

int day=10;int *ptrInt=&day;

printf("Show Address of the Variable\n");printf("\n%d",&day);printf("\n%d",ptrInt);

printf("\n\nShow value of the Variable\n");printf("\n%d",day);printf("\n%d",*ptrInt);

getch();}

Page 10: Programming In C++

Programming In C++, Lecture 7 By Umer Rana

Why Are Pointers Used?• To return more than one value from a function.• To pass array and strings more conveniently

from one function to another.• To manipulate arrays more easily by moving

pointers to them (or to parts of them), instead of moving the arrays themselves.

• To communicate information about memory, as in the function malloc(), which returns the location of free memory by using a memory.

Page 11: Programming In C++

Programming In C++, Lecture 7 By Umer Rana

Passing Addresses to a Functionvoid GiveValues(int *,int *);int main(void){

int day,day2;GiveValues(&day,&day2);

printf("\n Day Value is %d \n Day2 Value is %d",day,day2);getch();

}

void GiveValues (int *ptr1,int *ptr2){

*ptr1=10;*ptr2=20;

}

Page 12: Programming In C++

Programming In C++, Lecture 7 By Umer Rana

Pointer to Array Notation

Array Notation is really a thinly disguised form of pointer notation. Infect the compiler translates array notation into pointer notation because microprocessor understand pointers not arrays.

e.g. nums[] = {92,81,70,69,10};In Memory:nums[0]=92nums[1]=81nums[2]=70nums[3]=69nums[4]=10

Page 13: Programming In C++

Programming In C++, Lecture 7 By Umer Rana

Pointer to Array Notation

void main (void){

int nums[]={92,81,70,69,58};int i;for (i=0;i<5;i++)

printf(“%d\n”,nums[i]);}

Page 14: Programming In C++

Programming In C++, Lecture 7 By Umer Rana

Pointer to Array Notation

int nums[]={92,81,70,69,58};int i;printf("\nWith Array Notation \n");for (i=0;i<5;i++)

{ printf("%d\n",&nums[i]);printf("%d\n",nums[i]);

}printf("\nWith Pointer Notation\n");for (i=0;i<5;i++)

{ printf("%d\n",(nums+i));printf("%d\n",*(nums+i));

}

&nums[i] & nums[i] is Array Notation (nums+i) & *(nums+i) is Pointer Notation

Page 15: Programming In C++

Programming In C++, Lecture 7 By Umer Rana

Pointer to Array Notation

int temper[7];int day;printf("Please Enter the Temperature of the week\n");for (day=0;day<7;day++){printf("\nTemperature of day %d: ",(day+1));scanf("%d",temper + day); }for (day=0;day<7;day++){printf("\nTemperature of day %d is %d. ",(day+1),*(temper+day));}

Page 16: Programming In C++

Programming In C++, Lecture 7 By Umer Rana

Pointer to Array Functionint main(void){

int array[]={3,5,7,9,11};int addNum=10, size=5, i;

addcon(array,size,addNum);for(i=0;i<size;i++)printf("\n%d",*(array+i));

}void addcon(int *ptr,int size,int con){

int i;for (i=0;i<size;i++)

*(ptr+i)=*(ptr+i)+con;}

Page 17: Programming In C++

Programming In C++, Lecture 7 By Umer Rana

Pointer to String NotationStrings are arrays of character so String Notation is thinly disguised form of pointer notation. Infect the compiler translates string notation into pointer notation because microprocessor understand pointers .

Void main (void){

Char *salute = “Greetings”;Char name[50];Puts(“Enter your name: “);Gets(name);Puts(salute);Puts(name);

}

Page 18: Programming In C++

Programming In C++, Lecture 7 By Umer Rana

Pointer to String Notation{

char *name[4]={"Umer","Ali","Sarah","Adil"};

for(int i=0;i<4;i++){printf("%s\n",name[i]);

}getch();

}

We use here instead of two dimensional String

char names[MAX][SIZE];

We use Pointer char *name[4];

Pointer to string does not waste space between words.

Page 19: Programming In C++

Programming In C++, Lecture 7 By Umer Rana

Pointer to Pointer NotationPointers can be declared that point to other pointers. there is no limit to levels of indirection for a pointer type variable. An element of a two-dimensional array can be referenced with a pointer to a pointer.

int stud[4][2]={ {1,56},{2,44},{3,67},{4,77}};

int i;printf("\n Enterd Reg.No & Marks are");for (i=0;i<=3;i++){

printf("\n%d %d",*(*(stud+i)+0),*(*(stud+i)+1));}

*(*(stud+i)+0) = stud[i][0]*(*(stud+i)+1) = stud[i][1]

*(*(Row) + Colum)

Page 20: Programming In C++

Programming In C++, Lecture 7 By Umer Rana

Pointer to Pointer Notationint ROWS=4;Int COLS=5;

static int table [ROWS][COLS] = { {13,15,17,19,21},{20,22,24,26,28},{31,33,35,37,39},{40,42,44,46,48}

};Int j,k;For (j=0;j<ROWS;j++)

for (k=0;k<COLS; k++)*(*table+j) + k) = *( *(table+j) +k) +10;

Printf(“\n”);For (j=o;j<ROWS;j++){

for (k=0;k<COLS;k++)printf(“%d”, *( *(table+j) +k) );

Printf(“\n”);}}

Page 21: Programming In C++

Programming In C++, Lecture 7 By Umer Rana

Quiz

Q.1: which is correct way to define a pointer?a) int &ptr x ;b) int *ptr;c) *int ptr;

Q.2: In Expression int *ptr what has type int?d) Int Type variablee) The Address of ptrf) The variable pointed to by ptr

Q.2: What statement is missing?int j, *ptr;*ptr=3;

Page 22: Programming In C++

Programming In C++, Lecture 7 By Umer Rana

Quiz

Q.3 What will be the output?int arr[]={4,5,6};int i;for(int i=0;i<3;i++)

printf(“%d”,*(arr+i))Q.4 What will be the output?

int arr[]={4,5,6};int i;for(int i=0;i<3;i++)

printf(“%d”, arr+i)

Page 23: Programming In C++

Programming In C++, Lecture 7 By Umer Rana

Quiz

Q.5 What will be the output?

int arr[]={4,5,6};

int i, j=10, *ptr,*ptr2;

ptr2=&j;

ptr=arr;

printf(“%d”,*ptr2);

for(int i=0;i<3;i++)

printf(“%d”,*ptr++)