Address-of and Dereference Operators

Cards (11)

  • The address-of (&) is a symbol used to returns the memory address of a variable or operand.
    int *ptrInt = #
  • The address-of (&) is a symbol used to returns the memory address of a variable or operand.
    int *ptrInt = #
  • The address-of (&) is a symbol used to returns the memory address of a variable or operand.
    int *ptrInt = #
  • The dereference (*) is a symbol used to access the value stored at the memory address held by the pointer.
    *ptrInt = 10;
  • The dereference (*) is a symbol used to access the value stored at the memory address held by the pointer.
    *ptrInt = 10;
  • int num = 5;
    int *ptrInt = # //address of num will be assign to ptrInt variable
    cout << num << endl; //display the original value of num
    *ptrInt = 10; //use the dereference operator to change the value of the variable num
    cout << num << endl; //display the new value (10) of variable num
    cout << ptrInt << endl; //display the address of variable num
    cout << *ptrInt << endl; //access and display the value (10) of variable num
  • int num;
    int *ptrInt; ptrInt = &num; //address of num will be assign to ptrInt variable
    *ptrInt = 5; //use the dereference operator to assign a value to variable num
    cout << num << endl; //display the value of variable num
    cout << ptrInt << endl; //display the address of variable num
    cout << *ptrInt << endl; //access and display the value of variable num
  • int num = 5;
    int *ptrInt = &num; //address of num will be assign to ptrInt variable
    int **ptr = &ptrInt; // address of ptrInt will be assign to ptr variable
    cout << num << endl; //display the original value of num
    *ptrInt = 10; //use the dereference operator to change the value of the variable num
    cout << num << endl; //display the new value of variable num
    cout << ptrInt << endl; //display the address of variable num
    cout << *ptrInt << endl; //display the value of variable num
  • int *ptrInt = nullptr;
    /*ptrInt variable will be assign a null pointer or no memory address to be assigned */
    *ptrInt = 5; //use the dereference operator to change the value of unknown variable
    cout << ptrInt << endl; //will not display due to error
    cout << *ptrInt << endl; // will not display due to error
    /* This program will be terminated due to SEGMENTATION FAULT, because the program tries to access a memory location that is not allowed to access */
  • int *ptrInt = nullptr;
    /*ptrInt variable will be assign a null pointer or no memory address to be assigned */
    *ptrInt = 5; //use the dereference operator to change the value of unknown variable
    cout << ptrInt << endl; //will not display due to error
    cout << *ptrInt << endl; // will not display due to error
    /* This program will be terminated due to SEGMENTATION FAULT, because the program tries to access a memory location that is not allowed to access */
  • int *ptrInt = nullptr;
    /*ptrInt variable will be assign a null pointer or no memory address to be assigned */
    *ptrInt = 5; //use the dereference operator to change the value of unknown variable
    cout << ptrInt << endl; //will not display due to error
    cout << *ptrInt << endl; // will not display due to error
    /* This program will be terminated due to SEGMENTATION FAULT, because the program tries to access a memory location that is not allowed to access */