Looking for a Tutor Near You?

Post Learning Requirement ยป
x
x

Direction

x

Ask a Question

x

Hire a Tutor

Notes On Pointers C++

Loading...

Published in: Engineering
3,466 Views

11th and 12th grade and engineering students may find these presentations helpful to learn about individual topics.

Prerna S / Sharjah

4 years of teaching experience

Qualification: B.E in computer Science

Teaches: GRE, SAT, ACT, Maths, MS Office, Web Development, HTML, Java, English, Hindi, Science, Chemistry, Computer Science, Mathematics, English Language

Contact this Tutor
  1. Introduction to pointers
  2. variables ' a variable is a name for a piece of memory that holds a value. When our program instantiates a variable, a free memory address is automatically assigned to the variable, and any value we assign to the variable is stored in this memory address.
  3. The address-of operator (&) The address-of operator (&) allows us to see what memory address is assigned to a variable. int main() int x = 5; cout
  4. The dereference operator ( * ) ' cout
  5. pointers ' A pointer is a variable that holds a memory address as its value. ' Assigning a value to a pointer int value = 5; int *ptr = &value; // initialize ptr with address of variable value ptr 0012FF7C M emon/ Ad dress: 0012FF7C value 5
  6. Declaring a pointer int *iPtr; // a pointer to an integer value double *dPtr; // a pointer to a double value int int int * iPtr2; // also valid syntax (acceptable, but not favored) * iPtr3; // also valid syntax (but don't do this) *iPtr4, *iPtr5; // declare two pointers to integer variables
  7. #include int main() int value = 5; int *ptr = &value; // initialize ptr with address of variable value cout
  8. The type of the pointer has to match the type of the variable being pointed to: int iValue = 5; double dVaIue = 7.0; int *iPtr = &iValue; // ok double *dPtr = &dVaIue; // ok iPtr = &dValue; // wrong -- int pointer cannot point to the address of a double variable dPtr = &iValue; // wrong -- double pointer cannot point to the address of an int variable
  9. Note that the following is also not legal: int *ptr = 5; int value = 5; cout
  10. The above program prints: 0012FF7C 5 0012FF7C 5
  11. int valuel = 5; int value2 = 7 int *ptr; ptr = &valuel; // ptr points to valuel std::cout
  12. Reference variable ' So far, we've discussed two basic variable types: ' Normal variables, which hold values directly. ' Pointers, which hold the address of another value (or null) and can be dereferenced to retrieve the value at the address they point to. A reference (to a non-const value) is declared by using an ampersand (&) between the reference type and the variable name:
  13. References generally act identically to the values referencing. In this sense, a reference acts as an alias for the object being referenced. int main() int value = 5; // normal integer int &ref = value; // reference to variable value value = 6; // value is now 6 ref = 7; // value is now 7 std::cout
  14. Pass by value void foo(in y) std::cout
  15. // ref is a reference to the argument passed in, not a copy void changeN(int &ref) ref = 6; int main() int n = 5; cout