Public Group

#### Archived

This topic is now archived and is closed to further replies.

# Why exactly does this code work???

This topic is 5663 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Ok, this code works:
int _tmain(void)
{
const int max = 100;
long primes[max] = {2, 3, 5};
int count = 3;
long trial = 5;
bool isprime = true;

do
{
trial += 2;
int i = 0;

//try dividing the candidate by all the primes we have

do
{
isprime = trial % *(primes + i) > 0;
}while(++i < count && isprime);

if (isprime)
*(primes + count++) = trial;
}while(count < max);

for (int i = 0; i < max ; i++)
{
if (i % 5 == 0)
cout << endl;
cout << setw(10) << *(primes + i);
}
cout << endl;
return 0;
}


I don''t get it, when they referenced this: *(primes + i) they hadn''t declared "primes" as a pointer, only as a long value. Oh.. And btw this code is from a book in the context of teaching pointer!! BattleGuard My hobbies: Martial Arts, Computer/laptop, Listening to Linkin Park, Computer/laptop, programming, game-dev (that''s why the heck I''m on this site in the first place!!! And did I mention my computer/laptop???

##### Share on other sites
primes isn''t declared as a long, but as an array of longs. Array names are actually special pointers. I suggest you actually read the book though; it''ll tell you what''s going on.

##### Share on other sites
Primes is an array of long values. On it''s own, when not being referenced as an array, primes is just a pointer to the first item in the array. In other words,

primes

is the same thing as

&(primes[0])

##### Share on other sites
It's been debated rather heavily here as to whether or not it is actually a pointer.

I myself don't know for sure, but what I do know is that it CAN BE USED LIKE a pointer.

[edited by - Ronin Magus on June 18, 2003 5:00:26 PM]

##### Share on other sites
long primes[max] = {2, 3, 5};
declares an array of long values, not a long.
Arrays and pointers are (to some extend) the same, therefore
*(primes + i) is the same as primes[i]

***
For Java games and Java related resources, go to
http://www.javaengines.dk
***

[edited by - blackone on June 18, 2003 5:01:47 PM]

##### Share on other sites
Actually an array's name can be manipulated like a pointer to the first element in the array (it's not exactly the same, but the difference usually has no affect on your code). "*(primes + i)" is equivalent to the expression "primes[ i ]", and it probably should have been written that way. You might just want to do a text replace there because this example doesn't do anything special with using the array that way.

[EDIT] lol, I didn't even think about the [ i ] (minus the spaces) being the forum code for italics.

______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________

[edited by - Thunder_Hawk on June 18, 2003 5:02:41 PM]

##### Share on other sites
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.

##### Share on other sites
Agreed, I'm sure that code was in the book to show a point about pointers and arrays, not a suggestion for ever writing something like that. I'm pretty sure too that using that kind of syntax for referencing arrays is bad practice.

Edit: Heh Thunder, I made the same mistake;p

[edited by - blackone on June 18, 2003 5:05:04 PM]

##### Share on other sites
blackone, what''s the meaning of the a tag in your post? Is that just a hack?

##### Share on other sites
Arrays and pointers are very closely related in c/c++. For example when you create an array like so:

int Array[100] = {0};

the symbol ''Array'' is a pointer to the first element in the array (ie a pointer to the location in memory of Array[0]) This type of operation is very useful, especially for things like mucking around with strings (char arrays). Another example:

int i = 0;
int Array[100] = {0};
i = Array[20];

is the same as:

int i = 0;
int Array[100] = {0};
i = *(Array + 20);

Lets explain the second example:

(Array + 20) -- Start at the location of ''Array'' and move ahead 20 ints (since we know that ''Array is an int array)
* -- is the dereference operator. it retireves the value at the location of a pointer. Again, since we know ''Array is an int, it returns an int. It can also be typecast to another type if you want.

so in total, it says "Go to the memory location of ''Array'' move ahead 20 ints, then retrieve the value at that location in memory."

Don''t worry, this all will become second nature as you continue to learn. Arrays are your friend

Hope this explains it.

Ravyne
NYN Interactive Entertainment
HTTP://www.NYNInteractive.cjb.net

1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
5. 5
A4L
11

• 12
• 16
• 26
• 10
• 44
• ### Forum Statistics

• Total Topics
633768
• Total Posts
3013744
×