Why exactly does this code work???

Started by
12 comments, last by BattleGuard 20 years, 10 months ago
primes[0] = 2
primes[1] = 3
primes[2] = 5

if primes is the address of the first element in the array, &primes[0], then *primes is the value that is stored at primes[0], which is 2. the '*' operator dereferences the pointer so you can get the actual value, not the memory address.

the expression *(primes + i) means add i elements to the element primes is pointing to and dereference it, getting the value at the element pointed to by primes + i.

if primes is pointing to the first element, primes[0], then primes+1 = primes[1]. dereference primes[1], *primes, and you get the value 3.

-noix-

In this world gone mad, we won't spank the monkey; the monkey will spank us.

[edited by - noixtirdoe on June 18, 2003 5:09:38 PM]
In this world gone mad, we won't spank the monkey; the monkey will spank us.
Advertisement
quote:Original post by Tiffany Smith
Maybe im missing something but that code doesnt look like it would work to me because "setw" is not defined and there''s no header file.


cout is in there as well...It can be assumed he''s including iostream and has the line "using namespace std;" (or some equivalent construct)

______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
Ravyne, are you sure int Array[100] = {0}; is valid?

Developer journal: Multiplayer RPG dev diary

quote:Original post by blackone
Ravyne, are you sure int Array[100] = {0}; is valid?


Yep

at least in MS VC++ just tested it.

Ravyne
Owner/Lead Programmer & Designer
NYN Interactive Entertainment
HTTP://www.NYNInteractive.cjb.net

throw table_exception("(? ???)? ? ???");

This topic is closed to new replies.

Advertisement