Handout-6 CSM-1101.doc - ALIGARH MUSLIM UNIVERSITY...

Info icon This preview shows pages 1–5. Sign up to view the full content.

ALIGARH MUSLIM UNIVERSITY Department of Computer Science Course: MCA CSM-1101: Problem Solving Using C++ Academic Session 2017-2018 Handout-6 (Unit-IV) Topic: Use of pointers in C++. Teacher : Dr. Rafiqul Zaman Khan, Professor +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Objectives: To know what is pointer. To know why pointers are needed. To learn how to use pointes. To learn about pointer arithmetic. To learn how to return more than one values from the function using pointer. To learn how to use pointers with array. What is Pointer ? : Pointer variable is a special variable, which stores the address of other variable. If a pointer variable stores the address of a char variable, we call it a character pointer and so on. Normally a variable directly contains a specific value. A pointer, on the other hand, contains an address of a variable that contains a specific value. Pointers like any other variables must be declared before they can be used. To handle pointers in C++ language we use two unary operators: &, * & : Address operator (ampersand symbol) * : Pointer operator OR Indirection operator OR Value at address. Examples: int *j -------- à Means the value at address contained in j is an int OR in other words j is an integer pointer. Int **k ------ à Means the value at address contained in *k is an int. Pointers – Why? Using pointers allows us to: 1
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Achieve call by reference (i.e. write functions which change their parameters). We can return more than values from the function by using Pointers (output parameters) Handle arrays efficiently. Handle structures (records efficiently). Create liked lists, trees, graphs etc. Care must be taken when using pointers since there are no safety features. A pointer variable enables you to change the value of another variable without using that variable’s name. To learn about new and delete operators in C++ for dynamic memory Programming Examples on Pointer : To learn how to use & and * operators? 1) #include<iostream.h> void main( ) { int *a, b=10; a=&b; cout <<a<<endl<<endl; cout<<*a<<endl<<endl; cout<<b<<endl<<endl; } Output: 2) #include <iostream.h> 2
Image of page 2
int main() { int a; // a is an integer int *b; // b is a pointer to an integer a = 7; b = &a; // b assigned address of a cout <<"This Program is Written by Dr. Rafiq , CS Department, AMU "<<endl<<endl; cout << "The address of a is " << &a <<endl<<endl; cout << "The value of b is " << b <<endl<<endl; cout << "The value of a is " << a <<endl<<endl; cout<< "The value of *b is " << *b <<endl<<endl; return 0; // indicates successful termination } // end main What is printed by each of the following programs? a). #include <iostream.h> main() { int count = 10, x, *ip; ip = &count; x = *ip; cout<<"count = "<<count<<" x = "<<x<<endl; } b). #include <iostream.h> void main() { int i1, i2, *p1, *p2; i1 = 5; p1 = &i1; i2 = *p1 / 2 + 10; p2 = p1; cout<<"i1= "<<i1<<" i2= "<<i2<<" *p1= "<<*p1<<" *p2 = "<<*p2<<endl; } Pointer Arithmetic : 3
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

On incrementing a pointer, it points to the immediately next location of its type. The way a pointer can be incremented, it can be decremented as well, to point to earlier locations.
Image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern