1 / 13

Pointer Data Type and Pointer Variables

By: Nouf Aljaffan Edited by : Nouf Almunyif. Pointer Data Type and Pointer Variables. you learned how to declare pointer variables how to store the address of a variable into a pointer variable of the same type as the variable how to manipulate data using pointers

zamora
Download Presentation

Pointer Data Type and Pointer Variables

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. By:NoufAljaffan Editedby : NoufAlmunyif Pointer Data Type and Pointer Variables you learned how to declare pointer variables how to store the address of a variable into a pointer variable of the same type as the variable how to manipulate data using pointers you will learn how to allocate and deallocate memory during program execution using pointers.

  2. Pointer Data Type and Pointer Variables • C++’s data types are classified into three categories: • Simple • Structured • Pointers.

  3. What is the Pointer Value

  4. Pointer Data Type and Pointer Variables • There is no name associated with pointer data types • If no name is associated with a pointer data type, how do you declare pointer variables? • pis called a pointer variable of type int • chis called a pointer variable of type char.

  5. Pointer Declaration • Thus, the character *can appear anywhere between the data type name and the variable name.

  6. Pointer Declaration • int* p, q; • In this statement: • only p is the pointer variable, not q. • Here, q is an int variable. • we prefer to attach the character * to the variable name. So the preceding statement is written as: int*p, q;

  7. Pointer operator • C++ provides two operators operator to work with pointers. • (&) the address of operator • (*) the dereferencing

  8. Address of Operator (&) • is a unary operator that returns the address of its operand. • For example, given the statements: intx; int*p; • The statement: p = &x; • assigns the address of x to p. That is, x and the value of p refer to the same memory location

  9. Dereferencing Operator (*) • referred to as indirection operator • refers to the object to which its operand (that is, the pointer) points. • For example, given the statements: p X 25

  10. Dereferencing Operator (*) • Let us consider the following statements: In these statements: • pis a pointer variable of type int • numis a variable of type int. • Let us assume that memory location 1200 is allocated for p, and memory location 1800 is allocated for num.

  11. Dereferencing Operator (*) • Consider the following statements. • num= 78; • p = # • *p = 24;

  12. Example #include <iostream> #include <string> using namespace std; //=============================== int main() { int *p; int x = 37; cout <<"x= " << x<<endl; p=&x; cout <<"*p= "<< *p <<" , x= "<<x<<endl; *p=85; cout <<"*P= " <<*p <<" , x= "<<x<<endl; cout <<" Address of p is : "<< &p <<endl; cout<<"value of p : "<< p<<endl; cout<< " value of memory location pointed to by *p = "<<*p<<endl; cout<<"Address of X = " << &x<<endl; cout <<" Value of x = " << x<<endl; return 0; }

More Related