TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: Neil Heller
from: William McBrine
date: 2003-07-31 18:21:00
subject: Re: This always drives me cr

-=> Neil Heller wrote to All <=-

 NH> 1)  int * pIntArray = new int[100];
 NH> 2)  int IntArray[100];

 NH> If I wanted to place a value, say 1000, in the 7th position of #2, I
 NH> would go IntArray[7] = 1000.

 NH> If I wanted to place the same value in the same position of #1, how
 NH> would I go about doing it?

Same way:

 pIntArray[7] = 1000;

Think of both "IntArray" and "pIntArray" as pointers.
So when you say:

 pIntArray[7]

you're really saying:

 *(pIntArray + 7)

In fact, you can even do kinky shit like:

 7[pIntArray]

and get the same effect (implicit pointer arithmetic plus dereferencing).  
The array notation is just a convenience; pointers are all that's real.

 NH> How would I go about using memset() to set all elements of the array to
 NH> 0?

 memset(pIntArray, 0, sizeof(int) * 100);

although that implies contiguous storage of the array's elements, which I'm
not certain is guaranteed. In practice, alignment padding would never occur
_except_ on int boundaries, so it wouldn't happen here; but still, it might
be better not to assume:

 for (int x = 0; x < 100; x++)
     pIntArray[x] = 0;

And I'll bet you that's just as fast, if not faster (depending on compiler
optimizations and the implementation of memset()).

... Press any key to self destruct...
--- MultiMail/Linux v0.45
* Origin: COMM Port OS/2 juge.com 204.89.247.1 (281) 980-9671 (1:106/2000)
SEEN-BY: 633/267 270
@PATH: 106/2000 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.