Welcome to City-Data.com Forum!
U.S. CitiesCity-Data Forum Index
Go Back   City-Data Forum > General Forums > Science and Technology > Computers
 [Register]
Please register to participate in our discussions with 2 million other members - it's free and quick! Some forums can only be seen by registered members. After you create your account, you'll be able to customize options and access all our 15,000 new posts/day with fewer ads.
View detailed profile (Advanced) or search
site with Google Custom Search

Search Forums  (Advanced)
Reply Start New Thread
 
Old 06-04-2011, 02:28 PM
 
Location: Denver
690 posts, read 2,109,500 times
Reputation: 356

Advertisements

Can someone give a simple explanation for how a pointer works in a loop? For example, I don't understand what anything between "begin" and "end" means in this code:

[SIZE=2][SIZE=2]declare[/SIZE][/SIZE][SIZE=2] @Pointer [/SIZE][SIZE=2][SIZE=2]int
select
[/SIZE]
[/SIZE][SIZE=2] @Pointer [/SIZE][SIZE=2][SIZE=2]=[/SIZE][/SIZE][SIZE=2] 1[/SIZE]
[SIZE=2]
[/SIZE][SIZE=2][SIZE=2]while[/SIZE][/SIZE][SIZE=2] @Pointer [/SIZE][SIZE=2][SIZE=2]<[/SIZE][/SIZE][SIZE=2] 10
[/SIZE][SIZE=2][SIZE=2]begin
[/SIZE]
[/SIZE][SIZE=2][/SIZE][SIZE=2][SIZE=2] select[/SIZE][/SIZE][SIZE=2] Pointer [/SIZE][SIZE=2][SIZE=2]=[/SIZE][/SIZE][SIZE=2] @Pointer
[/SIZE][SIZE=2][SIZE=2] select[/SIZE][/SIZE][SIZE=2] @Pointer [/SIZE][SIZE=2][SIZE=2]=[/SIZE][/SIZE][SIZE=2] @Pointer [/SIZE][SIZE=2][SIZE=2]+[/SIZE][/SIZE][SIZE=2] 1
[/SIZE][SIZE=2][SIZE=2] if[/SIZE][/SIZE][SIZE=2] @Pointer [/SIZE][SIZE=2][SIZE=2]>[/SIZE][/SIZE][SIZE=2] 5
[/SIZE][SIZE=2][SIZE=2] break
end

select
[/SIZE]
[/SIZE][SIZE=2] @Pointer[/SIZE]

Will someone please break it down for me, if you understand it?
Reply With Quote Quick reply to this message

 
Old 06-04-2011, 02:30 PM
 
Location: Denver
690 posts, read 2,109,500 times
Reputation: 356
Hmm...that paste didn't work. Let me try again:

declare @Pointer int
select @Pointer = 1
while @Pointer < 10

begin
select Pointer = @Pointer
select @Pointer = @Pointer + 1
if @Pointer > 5
break
end
Reply With Quote Quick reply to this message
 
Old 06-04-2011, 03:38 PM
 
28,803 posts, read 47,742,521 times
Reputation: 37906
Am I doing your homework?

@Pointer is a variable. Int tells you it is an integer (number). It could MyFavoriteAlien instead of @Pointer and you'd get the same result, which is that the loop (there is no loop in the example though) runs until the value of the variable is greater than 5 then the loop stops and the program goes to the next portion of code if there is any. @Pointer is set to the value of 1 to start the loop.

In this case break tells the program to stop running.

While @Pointer is < 10 is a bit superfluous since it never gets larger than 6 in the loop.

I'm a bit rusty, but normally there is a while at the top of the loop and an end while at the bottom.

begin
select Pointer = @Pointer
select @Pointer = @Pointer + 1
if @Pointer > 5
break
end

As far as I can tell all this code is going to do is increment Pointer to the value of 2, and stop. There is no code to tell it to go to the top of the loop and loop through it again.

Something like this:

While Pointer < 5
select Pointer = @Pointer: I don't understand the need for this at all.
select @Pointer = @Pointer + 1: I don't know enough about the language used to understand the need for select
if @Pointer > 5
break
end while

If I wrote it it would be:

While @Pointer < 7
@Pointer = @Pointer + 1
if @Pointer > 5
break
end while

It doesn't feel right, though. It's been too long since I wrote code... It's not right. Someone who still codes can do a better job, I'm sure.

Last edited by Tek_Freek; 06-04-2011 at 03:59 PM..
Reply With Quote Quick reply to this message
 
Old 06-09-2011, 10:44 PM
 
3,614 posts, read 3,506,332 times
Reputation: 911
Quote:
Originally Posted by Tek_Freek View Post
Am I doing your homework?

@Pointer is a variable. Int tells you it is an integer (number). It could MyFavoriteAlien instead of @Pointer and you'd get the same result, which is that the loop (there is no loop in the example though) runs until the value of the variable is greater than 5 then the loop stops and the program goes to the next portion of code if there is any. @Pointer is set to the value of 1 to start the loop.

In this case break tells the program to stop running.

While @Pointer is < 10 is a bit superfluous since it never gets larger than 6 in the loop.

I'm a bit rusty, but normally there is a while at the top of the loop and an end while at the bottom.

begin
select Pointer = @Pointer
select @Pointer = @Pointer + 1
if @Pointer > 5
break
end

As far as I can tell all this code is going to do is increment Pointer to the value of 2, and stop. There is no code to tell it to go to the top of the loop and loop through it again.

Something like this:

While Pointer < 5
select Pointer = @Pointer: I don't understand the need for this at all.
select @Pointer = @Pointer + 1: I don't know enough about the language used to understand the need for select
if @Pointer > 5
break
end while

If I wrote it it would be:

While @Pointer < 7
@Pointer = @Pointer + 1
if @Pointer > 5
break
end while

It doesn't feel right, though. It's been too long since I wrote code... It's not right. Someone who still codes can do a better job, I'm sure.

What language is this?
Reply With Quote Quick reply to this message
 
Old 06-09-2011, 10:55 PM
 
24,488 posts, read 41,175,722 times
Reputation: 12921
As others have requested... can you specify which language this is?

Also, do you have an understanding of what a pointer is in general?
Reply With Quote Quick reply to this message
 
Old 06-09-2011, 10:58 PM
 
24,488 posts, read 41,175,722 times
Reputation: 12921
Quote:
Originally Posted by Tek_Freek View Post
Am I doing your homework?

@Pointer is a variable. It could MyFavoriteAlien instead of @Pointer and you'd get the same result
I believe @Pointer is a pointer. It could very well be @MyFavoriteAlien and you would get the same result. It could not be MyFavoriteAlien, because MyFavoriteAlien would be a variable, and not a pointer.

OP, please clarify that you are talking about pointers here and not variables.
Reply With Quote Quick reply to this message
 
Old 06-10-2011, 12:07 AM
 
2,245 posts, read 4,236,439 times
Reputation: 2155
Quote:
Originally Posted by NJBest View Post
As others have requested... can you specify which language this is?
Looks like pseudocode.
Reply With Quote Quick reply to this message
 
Old 06-10-2011, 01:58 AM
 
24,488 posts, read 41,175,722 times
Reputation: 12921
Quote:
Originally Posted by Visit a Library View Post
Looks like pseudocode.
I thought that could have been a possibility as well. But the usage of "Select" in this case is odd, so I wasn't sure.

So I made the assumption that "Select" meant "set" and wrote it out in C:

int * Pointer;
*Pointer = 1;

while (*Pointer < 10) {
Pointer = *Pointer; //This line is obviously wrong.
*Pointer++;
if (*Pointer > 5)
break;
}

Either I'm misinterpreting something, or the psuedocode has an error in it.

Last edited by NJBest; 06-10-2011 at 02:15 AM..
Reply With Quote Quick reply to this message
 
Old 06-10-2011, 04:38 AM
 
22,768 posts, read 30,763,698 times
Reputation: 14746
Quote:
Originally Posted by mjohnson4381 View Post
Can someone give a simple explanation for how a pointer works in a loop?
it is a data type, that points to another data type.

say you create an integer called "X", and set it equal to '5'

then you create a pointer called "Y", and set it equal to X.

if you were to change the value of integer X to '6', the value of pointer Y changes to '6' automatically.

a loop just makes some piece of code run, until something triggers that loop to stop.
Reply With Quote Quick reply to this message
 
Old 06-10-2011, 05:04 AM
 
24,488 posts, read 41,175,722 times
Reputation: 12921
Quote:
Originally Posted by le roi View Post
it is a data type, that points to another data type.

say you create an integer called "X", and set it equal to '5'

then you create a pointer called "Y", and set it equal to X.

if you were to change the value of integer X to '6', the value of pointer Y changes to '6' automatically.
Almost, but not quite accurate. You should clear it up so it's not confusing to the OP.
Reply With Quote Quick reply to this message
Please register to post and access all features of our very popular forum. It is free and quick. Over $68,000 in prizes has already been given out to active posters on our forum. Additional giveaways are planned.

Detailed information about all U.S. cities, counties, and zip codes on our site: City-data.com.


Reply
Please update this thread with any new information or opinions. This open thread is still read by thousands of people, so we encourage all additional points of view.

Quick Reply
Message:


Over $104,000 in prizes was already given out to active posters on our forum and additional giveaways are planned!

Go Back   City-Data Forum > General Forums > Science and Technology > Computers
Similar Threads

All times are GMT -6. The time now is 04:52 AM.

© 2005-2024, Advameg, Inc. · Please obey Forum Rules · Terms of Use and Privacy Policy · Bug Bounty

City-Data.com - Contact Us - Archive 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37 - Top