Upload
dana-east
View
214
Download
0
Embed Size (px)
Citation preview
Programming In C++
Spring Semester 2013Lecture 7
Programming In C++, Lecture 7 By Umer Rana
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.
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();
}
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;
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.
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();
}
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();}
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;
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();}
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.
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;
}
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
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]);}
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
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));}
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;}
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);
}
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.
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)
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”);}}
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;
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)
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++)