1 / 13

3 Review of basic structured programming

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

everly
Télécharger la présentation

3 Review of basic structured programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 3 Review of basic structured programming M Taimoor Khan taimoorkhan@ciit-attock.edu.pk

  2. 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

  3. variables #include<iostream.h> void main() { int a = 5; cout <<a <<endl; cout <<&a; }

  4. Pointers Declaration #include<iostream.h> void main() { int a; int b, c, d; int *ptr1; int *ptr2, *ptr3, *ptr4; }

  5. Accessing Pointers #include<iostream.h> void main() { int a = 6; int *ptr; ptr = &a; cout <<ptr<<endl; cout <<*ptr <<endl; }

  6. 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; }

  7. 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; }

  8. 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; }

  9. 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; }

  10. 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); }

  11. 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; }

  12. 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; }

  13. 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; }

More Related