![]() |
|
#1
|
|||
|
|||
can someone explain this code (Pointers and addresses)Here is the code. I ran it and also put the output below the code. I put in comments that will basically tell you what I know and what I dont udnerstand. I just need a thorough understanding of how the output was obtained. If it's easier for you to just write it out instead of writing in comments, please feel free to do so. I dont udnerstand pointers or address very well.
CPP / C++ / C Code:
/* a=0 a=1 a=2 a=3 a=4 a=5 address of a is 0012FF7C */ Last edited by dsmith : 10-Mar-2004 at 17:44.
Reason: Please use [c] and [/c] to highlight c code
|
|
#2
|
|||
|
|||
|
Code:
Yes: it says that aPtr is a pointer to an integer; furthermore its value is initialized to the address of the previously declared integer a. Code:
Read up on printf specifications: %d means print the decimal value of an integer. "What integer?" you ask. The integer which is pointed to by aPtr; that is it prints the current value of a. Since a is the loop counter, it prints the value of a on a new line for each pass of the loop. Since it prints a newline, \n, before and after the stuff on the line, there is a blank line between each printed line. Code:
Well, now it prints the value of the pointer aPtr in hexadecimal. This is the address assigned to the integer a that you declared in the beginning. Each compiler has its own way of handling address spaces, so we can't, in general, predict what its value will be. Sometimes in programs we mere humans get confused by certain C-language features, and it is helpful to know the values of certain pointers (or pointers to pointers, or pointers to arrays of chars, or ...). It can sometimes help us debug programs if we remember that we can look at values of pointers. Maybe, maybe not. But that's what it does. Good Luck! Dave |
Recent GIDBlog
Python ebook by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The