Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 135,947 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,725 people online right now. Registration is fast and FREE... Join Now!




Quick question on pointers.

 
Reply to this topicStart new topic

Quick question on pointers., pointers

inc0mplete
10 May, 2008 - 08:11 AM
Post #1

New D.I.C Head
*

Joined: 10 May, 2008
Posts: 22

for(ip = &ar[0]; ip < &ar[20]; ip++)
*ip = 0;


what does the ip < &ar[20] mean? I know this is a simple example of using pointers instead of indexing and this loops until the 20th index. but what does the &lt mean?
User is offlineProfile CardPM
+Quote Post

skaoth
RE: Quick Question On Pointers.
10 May, 2008 - 08:49 AM
Post #2

D.I.C Regular
Group Icon

Joined: 7 Nov, 2007
Posts: 342



Thanked: 10 times
Dream Kudos: 100
My Contributions
Well all its doing is comparing memory address. Lets look at an example.
Let's assume we have an array called ar with 5 elements

CODE

int ar[5] = {100, 101, 102, 103, 104};


This array will be located somewhere in memory as a contiguous
block. In this case 4 * 5 = 20 bytes total (4 bytes per int).
Each int in the array will have an address, so it will look something
like this in memory. (mem addresses are made up)

Address: 0x001 0x002 0x003 0x004 0x005
array idx: [0] [1] [2] [4] [4]
Value: 100 101 102 103 104

As shown in the table above the first value in the array
ar has an address of 0x001. Note: The address
scheme here is arbitrary, I increment by 1 just for simplicity.
Real memory addresses are offset by the size of the type.

In your code ip is assigned the value 0x001
or the address of the array ar at index 0.
The loop will continue until will the address in ip
has reached some point in the array. In this case 0x020 if we use
the addressing scheme above.

So in short. Its comparing addresses instead of the value of each
element in the array

hope that helps

Edit: D.I.C editor removing whitespace in my table sorry about its alignment

This post has been edited by skaoth: 10 May, 2008 - 08:52 AM
User is offlineProfile CardPM
+Quote Post

inc0mplete
RE: Quick Question On Pointers.
10 May, 2008 - 11:59 AM
Post #3

New D.I.C Head
*

Joined: 10 May, 2008
Posts: 22

QUOTE(skaoth @ 10 May, 2008 - 09:49 AM) *

Well all its doing is comparing memory address. Lets look at an example.
Let's assume we have an array called ar with 5 elements

CODE

int ar[5] = {100, 101, 102, 103, 104};


This array will be located somewhere in memory as a contiguous
block. In this case 4 * 5 = 20 bytes total (4 bytes per int).
Each int in the array will have an address, so it will look something
like this in memory. (mem addresses are made up)

Address: 0x001 0x002 0x003 0x004 0x005
array idx: [0] [1] [2] [4] [4]
Value: 100 101 102 103 104

As shown in the table above the first value in the array
ar has an address of 0x001. Note: The address
scheme here is arbitrary, I increment by 1 just for simplicity.
Real memory addresses are offset by the size of the type.

In your code ip is assigned the value 0x001
or the address of the array ar at index 0.
The loop will continue until will the address in ip
has reached some point in the array. In this case 0x020 if we use
the addressing scheme above.

So in short. Its comparing addresses instead of the value of each
element in the array

hope that helps

Edit: D.I.C editor removing whitespace in my table sorry about its alignment



that helped thank you. but what does the ip &lt do? it doesn't assign ip to anything, and there is no comparison symbol ie. (<, >, etc ) and how what is the relationship between 'ip &lt' and '&ar[20]'? &ar[20] just means the memory location of ar[20]. Unless i fully don't understand for loops. correct me if i am wrong.

an example of a simple for loop with no pointers would be
CODE

for (int i=0; i<20;i++){
;
}

the parameters declare an variable type int(used as a counter). Then the next parameter is a check statement. If i is less than 20 then loop. The next statement is increment i each loop.


Now with pointers. It declares a pointer and a memory location to that pointer. That fulfills the first parameter. Now the next parameter( usually the check statement) doesn't make sense. This is what i am confused about. How does ip &lt check to see if it is less than &ar[20]? I'm sorry if this is confusing. I am really trying to get a grasp of pointers and their use.

User is offlineProfile CardPM
+Quote Post

inc0mplete
RE: Quick Question On Pointers.
11 May, 2008 - 07:11 AM
Post #4

New D.I.C Head
*

Joined: 10 May, 2008
Posts: 22

bump.
User is offlineProfile CardPM
+Quote Post

Mystyco
RE: Quick Question On Pointers.
11 May, 2008 - 08:03 AM
Post #5

New D.I.C Head
*

Joined: 25 Oct, 2007
Posts: 3


My Contributions
The code Assing the memory address of ar[0] to the pointer ip (same as
CODE
ip = ar
) and set the value to 0 (*ip = 0;) as long as the pointer is pointing inside the array.

if you're refering to "&lt;", it means "<" or "less than". it got filtered tongue.gif

(sorry for the poor english)
User is offlineProfile CardPM
+Quote Post

inc0mplete
RE: Quick Question On Pointers.
11 May, 2008 - 08:13 AM
Post #6

New D.I.C Head
*

Joined: 10 May, 2008
Posts: 22

hahahaha ok! that makes more sense! i thought it was some weird C reserved word that i never knew about heh. makes perfect sense now.
Thank you!
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 08:54AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month