GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-Mar-2004, 17:22
tommy69 tommy69 is offline
Junior Member
 
Join Date: Mar 2004
Posts: 40
tommy69 is on a distinguished road

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:
int main()
{
     int a;		/*variable declared  */
     int *aPtr=&a; /* Does this say, "The variable pointed to by aPtr is an Integer? */

     for (a=0;a<5;a++)   /* The FOR loop will execute 5 times, (0,1,2,3,4) and is incremented by 1 */
     printf("\na=%d\n",a);	

     printf("a=%d\n",*aPtr);  /*I dont understand this */

     printf("address of a is %p\n",aPtr); /*Or this one */

     return 0;

}

/*



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  
Old 10-Mar-2004, 17:57
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,893
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Code:
int *aPtr=&a; /* Does this say, "The variable pointed to by aPtr is an Integer? */

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:
printf("\na=%d\n",*aPtr); /*I dont understand this */

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:
printf("address of a is %p\n",aPtr); /*Or this one */

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 GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:38.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.