|
|
Example:
|
/* ----------------------------------- Here are some (global) variables ----------------------------------- */ int i1, i2; short s1, s2; char c1, c2; float f1, f2; double d1, d2; int main(int argc, char *argv[]) { printf("Address of i1 = %u\n", &i1 ); // %u = unsigned integer printf("Address of i2 = %u\n", &i2 ); printf("Address of s1 = %u\n", &s1 ); printf("Address of s2 = %u\n", &s2 ); printf("Address of c1 = %u\n", &c1 ); printf("Address of c2 = %u\n", &c2 ); printf("Address of f1 = %u\n", &f1 ); printf("Address of f2 = %u\n", &f2 ); printf("Address of d1 = %u\n", &d1 ); printf("Address of d2 = %u\n", &d2 ); } |
Result:
Address of i1 = 134744 Address of i2 = 134748 (Notice: i2 located 4 bytes after i1) Address of s1 = 134752 Address of s2 = 134754 (Notice: s2 located 2 bytes after s1) Address of c1 = 134712 Address of c2 = 134713 (Notice: c2 located 1 bytes after c1) Address of f1 = 134736 Address of f2 = 134740 (Notice: f2 located 4 bytes after f1) Address of d1 = 134720 Address of d2 = 134728 (Notice: d2 located 8 bytes after d1) |
How to run the program:
|
|
|
Solution:
|
(dataType*) address |
meaning:
|
In other words:
(int*) 5000 means: 5000 is an address of an int typed variable (short*) 5000 means: 5000 is an address of an short typed variable (float*) 5000 means: 5000 is an address of an float typed variable |
* ( (int*) 5000 ) means: returns the content at address 5000 as an integer * ( (short*) 5000 ) means: returns the content at address 5000 as a short integer * ( (float*) 5000 ) means: returns the content at address 5000 as a float |
int i1 = 1234; short s1 = -54; float f1 = 3.14; int main(int argc, char *argv[]) { printf("&i1 (address of int var i1) = %u\n", (unsigned) &i1 ); printf("int value at 6295584 = %d\n", * ( (int*)6295584 ) ); printf("i1 = %d\n\n", i1); printf("&s1 (address of short var s1) = %u\n", (unsigned) &s1 ); printf("short value at 6295588 = %d\n", * ( (short*)6295588 ) ); printf("s1 = %d\n\n", s1); printf("&f1 (address of float var f1) = %u\n", (unsigned) &f1 ); printf("float value at 6295592 = %f\n", * ( (float*)6295592 ) ); printf("f1 = %f\n\n", f1); } |
Output:
&i1 (address of int var i1) = 6295584 int value at 6295584 = 1234 i1 = 1234 &s1 (address of short var s1) = 6295588 short value at 6295588 = -54 s1 = -54 &f1 (address of float var i1) = 6295592 float value at 6295592 = 3.140000 f1 = 3.140000 |
|
How to run the program:
|
Note:
|