Pointer Question

Programming, for all ages and all languages.
Post Reply
chris

Pointer Question

Post by chris »

As far as I know, the proper way to point a pointer to a variable is this:

Code: Select all


int *ptr;
int var = 1;

ptr = &var;

printf("%d", *ptr);

BUT, this seems to work to (although, it gives a warning):

Code: Select all


int *ptr;
int var = 1;

ptr = var;

printf("%d", ptr);

They both print "1". What is the difference?
sonneveld

Re:Pointer Question

Post by sonneveld »

in the first one you are assigning the address of 'var' to pointer 'ptr'. When you read *ptr later on, you are indirectly reading 1 from the address of var.

in the second one you are assigning the number '1' from 'var' to the pointer 'ptr'. To the pointer, it looks like it is pointing to the memory address 0x1. When you read the pointer later on, you don't have the indirect operator * so it just reads the address directly.

err.. i hope this makes sense.

- Nick
ark

Re:Pointer Question

Post by ark »

if that code only generates a warning, then you must be using C. In C++ that code should not compile at all, because you are trying to assign a value of type "int" to a variable of type "int*" without explicitly telling the compiler that you mean to do so.

This is also a nice example of how dangerous printf can be, since it doesn't lend itself to type checking in C or C++.

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);

Schol-R-LEA

Re:Pointer Question

Post by Schol-R-LEA »

A simpler demonstration of what is going on would be:

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);
... prints '5' (the current value of var, which ptr is pointing to), while

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() */
... prints '1'. This is because ptr was set to address 0x00000001 in the assignment statement, and then recast back to it's integer value, 1, by printf() - the function expects an integer, and it uses the value it is passed as an integer even if it wasn't one. HTH. C&CW.
chris

Re:Pointer Question

Post by chris »

Thanks to all of you, I understand now :)
Post Reply