Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Assigning Addresses to Pointers | Pointers Fundamentals
C++ Pointers and References

bookAssigning Addresses to Pointers

When working with pointers, proper initialization is important to ensure they point to valid memory locations and prevent unexpected behavior.

Address-of and Dereference operators

To assign a value to the pointer we have to use address-of operator and to access the value of the memory address we have to use dereference operator.

  • &: the address-of operator, returns the memory address of its operand;

  • *: the derefence operator, returns the value that stored in the memory address.

main.cpp

main.cpp

copy
12345678
#include <iostream> int main() { int variable = 10; std::cout << &variable << std::endl; std::cout << *(&variable) << std::endl; }

Using the address-of operator, we can assign these addresses to pointers, creating a direct link between the pointer and the memory location it points to.

Завдання

Swipe to start coding

In a banking system, each customer has an account balance. Transactions such as deposits or withdrawals can change this balance. However, withdrawals cannot make the balance negative. Write a program that updates and manages the account balance using pointers.

You will implement a function updateBalance that performs the transaction by modifying the balance through a pointer.

  1. Inside the updateBalance function, check if the transaction is a withdrawal that would result in a negative balance:
    • If amount is negative and *p_balance + amount < 0, the transaction is not allowed.
    • Return false in this case.
  2. If the transaction is allowed, update the balance by adding amount to *p_balance using pointer dereferencing.
  3. Return true to indicate a successful transaction.

Рішення

solution.cpp

solution.cpp

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 2
single

single

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

close

Awesome!

Completion rate improved to 5.88

bookAssigning Addresses to Pointers

Свайпніть щоб показати меню

When working with pointers, proper initialization is important to ensure they point to valid memory locations and prevent unexpected behavior.

Address-of and Dereference operators

To assign a value to the pointer we have to use address-of operator and to access the value of the memory address we have to use dereference operator.

  • &: the address-of operator, returns the memory address of its operand;

  • *: the derefence operator, returns the value that stored in the memory address.

main.cpp

main.cpp

copy
12345678
#include <iostream> int main() { int variable = 10; std::cout << &variable << std::endl; std::cout << *(&variable) << std::endl; }

Using the address-of operator, we can assign these addresses to pointers, creating a direct link between the pointer and the memory location it points to.

Завдання

Swipe to start coding

In a banking system, each customer has an account balance. Transactions such as deposits or withdrawals can change this balance. However, withdrawals cannot make the balance negative. Write a program that updates and manages the account balance using pointers.

You will implement a function updateBalance that performs the transaction by modifying the balance through a pointer.

  1. Inside the updateBalance function, check if the transaction is a withdrawal that would result in a negative balance:
    • If amount is negative and *p_balance + amount < 0, the transaction is not allowed.
    • Return false in this case.
  2. If the transaction is allowed, update the balance by adding amount to *p_balance using pointer dereferencing.
  3. Return true to indicate a successful transaction.

Рішення

solution.cpp

solution.cpp

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 2
single

single

some-alt