POINTER

Cards (21)

  • Pointer
    A variable that holds the address of another variable
  • A pointer is declared using the * operator before an identifier
  • Pointer can only 'point' to the value with the same data type
  • Why use pointers
    • If we want to pass a huge struct or array, it's easier to pass a pointer than the whole thing
    • Pointers allow cleaner, more compact code
  • Assigning the address of variable to pointer
    1. C++ allows two ways of accessing variables: Name (Identifier) and Address (Pointer)
    2. Symbol & gets the address of the variable that follows it
    3. Addresses can be displayed by the cout statement
    4. Addresses displayed in HEXADECIMAL
  • Pointer initialization
    • int myvar = 10;
    int * myptr = &myvar;
    int myvar = 10;
    int * myptr;
    myptr = &myvar;
  • Pointer Operators
    *: Dereferencing operator - To get the value stored in the memory address
    &: Reference operator / Address-of - Returns the memory address of its operand
  • Pointer Operators
    • If a number variable is stored in the memory address 0x123, and it contains a value 5. The reference (&) operator gives the value 0x123, while the dereference (*) operator gives the value 5.
  • Using pointers in C++
    • Define a pointer variable
    2. Assign the address of a variable to a pointer
    3. Access the value at the address available in pointer
  • Using pointers in C++

    • Example 1:
    int *p, var=101;
    p = &var;
    cout<<*p;
    Output: 101
  • Using pointers in C++

    • Example 2:
    int firstvalue, secondvalue;
    int * mypointer;
    mypointer = &firstvalue;
    *mypointer = 100;
    mypointer = &secondvalue;
    *mypointer = 200;
    cout << "firstvalue is " << firstvalue << '\n';
    cout << "secondvalue is " << secondvalue << '\n';
    Output: firstvalue is 100, secondvalue is 200
  • Using pointers in C++
    • Example 3:
    int *pc, c;
    c = 5;
    cout << "Address of c (&c): " << &c << endl;
    cout << "Value of c (c): " << c << endl << endl;
    pc = &c; // Pointer pc holds the memory address of variable c
    cout << "Address that pointer pc holds (pc): "<< pc << endl;
    cout << "Content of the address pointer pc holds (*pc): " << *pc << endl << endl;
    c = 11; // The content inside memory address &c is changed from 5 to 11.
    cout << "Address pointer pc holds (pc): " << pc << endl;
    cout << "Content of the address pointer pc holds (*pc): " << *pc << endl << endl;
    *pc = 2;
    cout << "Address of c (&c): " << &c << endl;
    cout << "Value of c (c): " << c << endl << endl;
  • Using pointers in C++

    • Example 4:
    int firstvalue = 5, secondvalue = 15;
    int * p1, * p2;
    p1 = &firstvalue; // p1 = address of firstvalue
    p2 = &secondvalue; // p2 = address of secondvalue
    *p1 = 10; // value pointed to by p1 = 10
    *p2 = *p1; // value pointed to by p2 = value pointed to by p1
    p1 = p2; // p1 = p2 (value of pointer is copied)
    *p1 = 20; // value pointed to by p1 = 20
    cout << "firstvalue is " << firstvalue << '\n';
    cout << "secondvalue is " << secondvalue << '\n';
  • New Operator
    The "new" operator allocates memory for a variable or any other entity on a heap.
  • New Operator
    • int *ptr = NULL; ptr = new int;
    int *p = new int(25);
    float *q = new float(75.25);
  • Delete Operator
    The memory allocated dynamically using the new operator has to be freed explicitly by the programmer. For this purpose, we are provided with the "delete" operator.
  • Relationship between pointer and array
    Pointers are the variables that hold address. Not only can pointers store address of a single variable, it can also store address of cells of an array.
  • While assigning the address of array to pointer don't use ampersand sign(&)
  • Traversing the array using Pointers
    • int *p; int arr[]={1, 2, 3, 4, 5, 6}; p = arr; for(int i=0; i<6;i++){ cout<<*p<<endl; p++;}
  • Access Array Elements Using Pointer
    • int data[5], *ptr; ptr = data; cout << "Enter elements: "; for(int i = 0; i < 5; ++i) cin >> data[i]; cout << "You entered: "; for(int i = 0; i < 5; ++i) cout << endl << *(ptr + i);
  • Display address of elements of an array using both array and pointers
    • float arr[5]; float *ptr; cout << "Displaying address using arrays: " << endl; for (int i = 0; i < 5; ++i) { cout << "&arr[" << i << "] = " << &arr[i] << endl; } ptr = arr; // ptr = &arr[0] cout<<"\nDisplaying address using pointers: "<< endl; for (int i = 0; i < 5; ++i) { cout << "ptr + " << i << " = "<< ptr + i << endl; }