The dereference operator. In C, we use the * operator for this. Note that the * symbol has several meanings in C (multiplication, pointer declaration and pointer dereferencing). The meaning is determined from the context in which it is used.
int x, y; /* integer variables */
int* p; /* pointer variable (pointer to int) */
x = 42; /* assign a value to the x variable */
p = &x; /* assign the address of the x variable to the pointer variable p */
y = *p; /* assign the value pointed to by p (42) to the variable y */
Chat with our AI personalities
Under C, the address of a variable or a function can be obtained by prefixing the name with an ampersand (&), which is called a "reference operator". The address is typically stored in a pointer variable. To access the value in a pointer variable, an asterisk (*) is used, which is called a "dereference operator".
For instance:
void dosomethinguseless()
{
int x=50;
int *y;
y=&x;
printf("%d, %d\n", x, *y);
}
- First, an integer variable called "x" is declared and preloaded with the value "50".
- Next, a variable called "y" is declared as an integer pointer.
- After that, the address of "x" is stored into "y", so that "y" now points to the address where the value of "x" is stored.
- Finally, both variables are printed to demonstrate that they, in one fashion or another, represent the value "50".
See the related link below for more information on C pointers.
In C we use & operator while giving address of some variable to some pointer variable. & operator is also used in scanf().
// Use the & operator (Sometimes called the "address of" operator int variable = 7; printf("Address of variable = %d\n", &variable); printf("Value of variable = %d\n", variable);
Use the address-of operator: char c=32; // space character std::cout<<&c<<std::endl;
A pointer variable contains the address to some memory location. "Dereferencing" the pointer means getting the value stored at that memory location.
To access a hidden global variable, use the scope resolution operator ::