130 likes | 257 Vues
This review introduces the fundamentals of pointers in C++. It explains what pointers are and their significance in programming, focusing on storing addresses instead of values. The text covers accessing array elements, passing arguments to functions by reference, and how to handle arrays and strings using pointers. Practical examples demonstrate the declaration, access, and usage of pointers, including pointer arithmetic, function parameters, and working with constant strings. Ideal for beginners looking to grasp basic structured programming concepts.
E N D
3 Review of basic structured programming M Taimoor Khan taimoorkhan@ciit-attock.edu.pk
Pointers • What are Pointers • They store addresses like variables store values • Why do we need them • Accessing array elements • Passing argument to functions when they need the original copy • Passing arrays and strings to functions • Creating data structures
variables #include<iostream.h> void main() { int a = 5; cout <<a <<endl; cout <<&a; }
Pointers Declaration #include<iostream.h> void main() { int a; int b, c, d; int *ptr1; int *ptr2, *ptr3, *ptr4; }
Accessing Pointers #include<iostream.h> void main() { int a = 6; int *ptr; ptr = &a; cout <<ptr<<endl; cout <<*ptr <<endl; }
Using Pointers #include<iostream.h> void main() { int a = 5; float b = 4.4; double c = 1.345; int *ptr1; ptr1 = &a; float *ptr2; ptr2 = &b; double *ptr3; ptr3 = &c; cout <<ptr1 <<“\t” <<*ptr1<<endl; cout <<ptr2<<“\t” <<*ptr2<<endl; cout <<ptr3<<“\t” <<*ptr3<<endl; }
Pointers and Arrays #include<iostream.h> void main() { intarr[] = {7, 4, 3, 5, 2}; for(inti=0; i<5; i++) cout <<arr[i] <<“\t”; cout<<endl; int *ptr; ptr = arr; for(inti=0; i<5; i++) cout <<ptr+i<<“\t”<<*(ptr+i)<<endl; }
Pointers and Arrays #include<iostream.h> void main() { intarr[] = {7, 4, 3, 5, 2}; for(inti=0; i<5; i++) cout <<arr[i] <<“\t”; cout<<endl; int *ptr; ptr = arr; for(inti=0; i<5; i++) cout <<*ptr++<<endl; }
Pointers to Functions #include<iostream.h> void halfValue(int *v2) { *v2 = *v2 /2; } void main() { int score = 7; int *ptr; ptr = &score; halfValue(ptr); cout<<ptr<<endl; cout <<*ptr<<endl; cout <<score<<endl; }
Array Pointers to Functions #include<iostream.h> Int MAX = 5; void add(int *arrPointer) { int sum = 0; for(inti=0; i<MAX; i++) sum += *arrPointer++; cout <<“Total: “<<sum; } void main() { intarr[MAX] = {4, 3, 6, 8, 1}; int *ptr; ptr = arr; add(ptr); }
Pointer to Constant String #include<iostream.h> void main() { char str = “This is a constant string”; char *ptr; ptr = str; cout<<str<<endl; cout <<ptr<<endl; cout<<*ptr<<endl; cout<<*ptr+1<<endl; cout <<*(ptr+1)<<endl; }
Pointers to Character Arrays #include<iostream.h> void main() { char str[] = {‘a’, ‘b’, ‘d’, ‘f’}; char *ptr; ptr = str; cout<<str<<endl; cout <<ptr<<endl; cout<<*ptr<<endl; cout<<*ptr+1<<endl; cout <<*(ptr+1)<<endl; }
Special Pointer Type #include<iostream.h> void main() { int a = 6; float b = 3.2; void *ptr; ptr = &a; cout <<ptr<<endl; ptr = &b; cout <<ptr<<endl; }