230 likes | 388 Views
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.
E N D
Programming In C++ Spring Semester 2013 Lecture 7 Programming In C++, Lecture 7 By UmerRana
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. 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. Programming In C++, Lecture 7 By UmerRana
Pointers 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) { intv; printf("%d",&v); getch(); } Programming In C++, Lecture 7 By UmerRana
Pointers Declaration 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; Programming In C++, Lecture 7 By UmerRana
Pointers Notation 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, *ptrIntand v both represent the same data item. Programming In C++, Lecture 7 By UmerRana
Pointers Show Address int main(void) { intday; int *ptrInt; ptrInt=&day; printf("%d",&day); printf("\n%d",ptrInt); getch(); } Programming In C++, Lecture 7 By UmerRana
Pointers Show Values intmain(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(); } Programming In C++, Lecture 7 By UmerRana
Pointers Initializing 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; Programming In C++, Lecture 7 By UmerRana
Pointers Initializing intmain(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 UmerRana
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 UmerRana
Passing Addresses to a Function void 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 UmerRana
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]=92 nums[1]=81 nums[2]=70 nums[3]=69 nums[4]=10 Programming In C++, Lecture 7 By UmerRana
Pointer to Array Notation void main (void) { intnums[]={92,81,70,69,58}; int i; for (i=0;i<5;i++) printf(“%d\n”,nums[i]); } Programming In C++, Lecture 7 By UmerRana
Pointer to Array Notation intnums[]={92,81,70,69,58}; inti; printf("\nWith Array Notation \n"); for (i=0;i<5;i++) { printf("%d\n",&nums[i]); printf("%d\n",nums[i]); } printf("\nWithPointer 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 UmerRana
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 UmerRana
Pointer to Array Function intmain(void) { intarray[]={3,5,7,9,11}; intaddNum=10, size=5, i; addcon(array,size,addNum); for(i=0;i<size;i++) printf("\n%d",*(array+i)); } void addcon(int *ptr,intsize,int con) { int i; for (i=0;i<size;i++) *(ptr+i)=*(ptr+i)+con; } Programming In C++, Lecture 7 By UmerRana
Pointer to String Notation Strings 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 UmerRana
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 UmerRana
Pointer to Pointer Notation Pointers 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 EnterdReg.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 UmerRana
Pointer to Pointer Notation int 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} }; Intj,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 UmerRana
Quiz • Q.1: which is correct way to define a pointer? • int &ptr x ; • int *ptr; • *intptr; • Q.2: In Expression int *ptr what has type int? • Int Type variable • The Address of ptr • The variable pointed to by ptr • Q.2: What statement is missing? • int j, *ptr; • *ptr=3; Programming In C++, Lecture 7 By UmerRana
Quiz Q.3 What will be the output? intarr[]={4,5,6}; int i; for(int i=0;i<3;i++) printf(“%d”,*(arr+i)) Q.4 What will be the output? intarr[]={4,5,6}; inti; for(int i=0;i<3;i++) printf(“%d”, arr+i) Programming In C++, Lecture 7 By UmerRana
Quiz Q.5 What will be the output? intarr[]={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++) Programming In C++, Lecture 7 By UmerRana