|
|
For any variable x:
*(&x) ≡ x !!!
|
Example:
#include <stdio.h>
int main(int argc, char *argv[])
{
int i = 4;
printf("&i = %u\n", &i ); // I SHOULD use %p, but I like %u
// I can live with a warning...
printf(" i = %d\n", i ); // %d = signed integer
*(&i) = 9999; // * and & are each other's reverse !
// This statement is same as: i = 9999;
printf(" i = %d\n", i ); // %d = signed integer
}
|
Output:
cs255-1@aruba (5777)> a.out &i = 2717581396 i = 4 i = 9999 |
How to run the program:
|
|
|
#include <stdio.h>
int main(int argc, char *argv[])
{
int i1 = 4, i2 = 88;
int *p;
printf("Address of i1 = %u\n", &i1 ); // %u = unsigned integer
printf("Address of i2 = %u\n\n", &i2 );
p = &i1; // p points to variable i1
printf("Value of p = %u\n", p );
printf("Value of *p = %d\n\n", *p ); // *p is (now) alias for i1
p = &i2; // p points to variable i2
printf("Value of p = %u\n", p );
printf("Value of *p = %d\n\n", *p ); // *p is (now) alias for i2
}
|
Output:
Address of i1 = 4290768712 Address of i2 = 4290768708 Value of p = 4290768712 Value of *p = 4 Value of p = 4290768708 Value of *p = 88 |
How to run the program:
|
|
Illustration:
int i1;
int i2;
int *p;
p = &i1; // p points to variable i1
*p = 1234; // Stores the value 1234 in the variable
// pointed to by p (= i1)
|
int main(int argc, char *argv[])
{
int i1 = 4, i2 = 88;
int *p;
printf("Value of i1 = %d\n", i1 );
p = &i1; // p points to variable i1
*p = 1234; // Store 1234 in i1 !!!
printf("Value of i1 = %d\n", i1 );
printf("Value of i2 = %d\n", i2 );
p = &i2; // p points to variable i2
*p = 9999; // Store 9999 in i2 !!!
printf("Value of i2 = %d\n", i2 );
}
|
Output:
Value of i1 = 4 Value of i1 = 1234 (i1 is changed !!!) Value of i2 = 88 Value of i2 = 9999 (i2 is changed !!!) |
How to run the program:
|
|
Example:
BankAccount x = new BankAccount();
BankAccount y;
y = x; // y is now an alias for x
|
int i1;
int *p;
p = &i1; // *p is now an alias for i1
|
|
Example:
movw r0, #:lower16:x
movt r0, #:upper16:x // r0 = address of variable x
ldr r1. [r0] // r0 acts like a reference variable !!!
.data
x: .skip 4
|
The C reference variable p contains the address of x (= 5000)
The ARM register r0 contains the address of x (= 5000)
Then:
C expression Similar to ARM
---------------------------------------------------
p r0 (= value in reg r0)
*p [r0] (= value in memory location at addr r0)
|
|
|