OSDev.org

The Place to Start for Operating System Developers
It is currently Sat May 04, 2024 12:54 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Pointer Question
PostPosted: Tue Jan 20, 2004 6:50 pm 
As far as I know, the proper way to point a pointer to a variable is this:

Code:

int *ptr;
int var = 1;

ptr = &var;

printf("%d", *ptr);



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

Code:

int *ptr;
int var = 1;

ptr = var;

printf("%d", ptr);



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


Top
  
 
 Post subject: Re:Pointer Question
PostPosted: Tue Jan 20, 2004 7:46 pm 
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


Top
  
 
 Post subject: Re:Pointer Question
PostPosted: Wed Jan 21, 2004 12:01 am 
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:
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:
// 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);



Top
  
 
 Post subject: Re:Pointer Question
PostPosted: Wed Jan 21, 2004 1:57 am 
A simpler demonstration of what is going on would be:

Code:
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:
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.


Top
  
 
 Post subject: Re:Pointer Question
PostPosted: Wed Jan 21, 2004 8:25 am 
Thanks to all of you, I understand now :)


Top
  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group