Code: Select all
int *ptr;
int var = 1;
ptr = &var;
printf("%d", *ptr);
Code: Select all
int *ptr;
int var = 1;
ptr = var;
printf("%d", ptr);
Code: Select all
int *ptr;
int var = 1;
ptr = &var;
printf("%d", *ptr);
Code: Select all
int *ptr;
int var = 1;
ptr = var;
printf("%d", ptr);
Code: Select all
int* ptr;
int var = 1;
// here, ptr points to var, which has the value 1;
// however, ptr itself does NOT have the value 1
ptr = &var;
// so, we can say this:
assert(*ptr == 1);
assert(ptr == &var);
// but we CANNOT say this:
assert(ptr == 1);
// and then if we say this:
var = 5;
// we can still say:
assert(*ptr == var);
Code: Select all
// BY THE WAY: don't bother trying this one in a compiler;
// it will most likely crash, because ptr is not valid
int* ptr;
int var = 1;
// here, ptr receives the value of var, but ptr does
// not point to var
ptr = var;
// we can say this:
assert(ptr == var);
// but we can't say this:
assert(*ptr == var);
// and if we say this:
var = 5;
// then we can now say this:
assert(ptr == 1 && var == 5);
assert(*ptr != var);
assert(ptr != var);
// but we can't say this:
assert(ptr == var);
assert(*ptr == var);
Code: Select all
int *ptr;
int var = 1;
ptr = &var; /* ptr set to the address of var, whatever that is */
var = 5; /* reassigning var to 5 */
printf("%d", *ptr);
Code: Select all
int *ptr;
int var = 1;
ptr = var; /* implicit (int *) cast, int 1 become address 0x00000001 */
var = 5; /* reassigning var to 5 */
printf("%d", ptr); /* ptr implicitly recast back to int in printf() */