Jump to content
  • Advertisement

Archived

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

BattleGuard

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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
Owner/Lead Programmer & Designer
NYN Interactive Entertainment
HTTP://www.NYNInteractive.cjb.net

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!