Operations Pointer Variables

Cards (10)

  • The pointer variables in C++ can perform certain operations such as limited arithmetic and relational operations.
  • The pointer variables in C++ can perform certain operations such as limited arithmetic and relational operations.
  • Common Arithmetic Operations:
    • Increment (++)
    • Decrement (--)
    • Addition (pointer + n) or (n + pointer)
    • Subtraction (pointer - n) (pointer - pointer)
  • Common Arithmetic Operations:
    • Increment (++)
    • Decrement (--)
    • Addition (pointer + n) or (n + pointer)
    • Subtraction (pointer - n) (pointer - pointer)
  • Common Arithmetic Operations:
    • Increment (++)
    • Decrement (--)
    • Addition (pointer + n) or (n + pointer)
    • Subtraction (pointer - n) (pointer - pointer)
  • Relational operations:
    • Equality Operator (==)
    • Inequality Operator (!=)
    • Less Than Operator (<)
    • Greater Than Operator (>)
    • Less Than or Equal To Operator (<=)
    • Greater Than or Equal To Operator (>=)
  • Relational operations:
    • Equality Operator (==)
    • Inequality Operator (!=)
    • Less Than Operator (<)
    • Greater Than Operator (>)
    • Less Than or Equal To Operator (<=)
    • Greater Than or Equal To Operator (>=)
  • Relational operations:
    • Equality Operator (==)
    • Inequality Operator (!=)
    • Less Than Operator (<)
    • Greater Than Operator (>)
    • Less Than or Equal To Operator (<=)
    • Greater Than or Equal To Operator (>=)
  • int numArr[] = {1, 2, 3, 4, 5};
    int *ptrArray = numArr; // assign the address of the first element to ptrArray
    cout << "Element in index 0: " << *ptrArray << endl; //Output 1, index 0
    ptrArray++; // ++ptrArray;
    cout << "Element in index 1: " << *ptrArray << endl; //Output 2, index 1
    cout << "The element is: " << ptrArray[1] << endl; //Output 3, index 2
    *ptrArray += 5;
    cout << "The value is: " << *ptrArray << endl; //Output 7, index 1
    ptrArray += 3;
    cout << "Element in index 4: " << *ptrArray << endl; //Output 5, index 4
  • int numArr[] = {1, 2, 3, 4, 5};
    int *ptrArray = &numArr[2];
    // assign the address of the numArr index 2 to ptrArray pointer
    cout << *ptrArray << endl; //display the value in index 2, which is 3
    cout << ptrArray[2] << endl; //display the value in index 4, which is 5
    //ptrArray[2] is equivalent to *(ptrArray + 2)
    cout << *ptrArray << endl; // display the value in index 2, which is 3
    ptrArray = &ptrArray[2]; //will add 2 to the index
    cout << *ptrArray << endl; // display the value in index 4, which is 5