Computer Science, asked by aleeshashaji000, 9 months ago

C++ uses
as the address
operator
O #
0&
O*

Answers

Answered by akbarhussain26
2

Answer:

below two operators.

To access address of a variable to a pointer, we use the unary operator & (ampersand) that returns the address of that variable. For example &x gives us address of variable x.

// The output of this program can be different

// in different runs. Note that the program

// prints address of a variable and a variable

// can be assigned different address in different

// runs.

#include <stdio.h>

int main()

{

int x;

// Prints address of x

printf("%p", &x);

return 0;

}

One more operator is unary * (Asterisk) which is used for two things :

To declare a pointer variable: When a pointer variable is declared in C/C++, there must be a * before its name.

// C program to demonstrate declaration of

// pointer variables.

#include <stdio.h>

int main()

{

int x = 10;

// 1) Since there is * in declaration, ptr

// becomes a pointer varaible (a variable

// that stores address of another variable)

// 2) Since there is int before *, ptr is

// pointer to an integer type variable

int *ptr;

// & operator before x is used to get address

// of x. The address of x is assigned to ptr.

ptr = &x;

return 0;

}

To access the value stored in the address we use the unary operator (*) that returns the value of the variable located at the address specified by its operand. This is also called Dereferencing.

C++

// C++ program to demonstrate use of * for pointers in C++

#include <iostream>

using namespace std;

int main()

{

// A normal integer variable

int Var = 10;

// A pointer variable that holds address of var.

int *ptr = &Var;

// This line prints value at address stored in ptr.

// Value stored is value of variable "var"

cout << "Value of Var = "<< *ptr << endl;

// The output of this line may be different in different

// runs even on same machine.

cout << "Address of Var = " << ptr << endl;

// We can also use ptr as lvalue (Left hand

// side of assignment)

*ptr = 20; // Value at address is now 20

// This prints 20

cout << "After doing *ptr = 20, *ptr is "<< *ptr << endl;

Similar questions