Skip to main content
gamedev.net
🔒 Locked

pointer source code doesnt do anything!

Started by BlaDe16 11 replies 1.8k views
Original Post
BlaDe16
BlaDe16
Hi, someone was helping me and gave me this line of code as an example of a pointer in a for loop: for(int* ArrayPointer = array; ArrayPointer < array + size; ++ArrayPointer) cout<<(*ArrayPointer); I wrote up code based on it in hopes I would see how a pointer would work in action and it does nothing when i run my code. I thought it was going to use the pointer ArrayPointer to loop through each record in the array and cout it to the screen, why this is any different than just using a standard for loop im not sure yet, but any help is appreciated. #include <iostream> #include "FUNCTIONS.H" using namespace std; void PointerForLoop() { int Array[10] = {0,1,2,3,4,5,6,7,8,9}; int* ArrayPointer; for(int* ArrayPointer = Array; ArrayPointer < Array; ++ArrayPointer) cout<<(*ArrayPointer); PressAnyKey(); MainMenu(); }
Tromack
Tromack
Your problem is that you didn't just copy and paste what they gave you.

for(int* ArrayPointer = array; ArrayPointer < array + size; ++ArrayPointer) cout<<(*ArrayPointer);

is not the same as

for(int* ArrayPointer = Array; ArrayPointer < Array; ++ArrayPointer) cout<<(*ArrayPointer);

You need to have ArrayPointer < Array + size (in this case 10) not ArrayPointer < Array.
The way you have it now you set the ArrayPointer to the beginning of Array and then instantly check to see if the pointer is before Array.
Molle85
Molle85
An extremely ugly way to step through an array imo
CompEngRH
CompEngRH
First, you don't need to declare ArrayPointer twice. Second you weren't actually entering the for loop. (Note the Array +10)


#include <iostream>using namespace std;int main(int argc, char * argv[]){        int Array[10] = {0,1,2,3,4,5,6,7,8,9};        for(int* ArrayPointer = Array; ArrayPointer < Array + 10; ++ArrayPointer)        {                cout << "Val= " << (*ArrayPointer) << endl;        }return 0;}
gregrampage
gregrampage
Quote:
Original post by BlaDe16
I wrote up code based on it in hopes I would see how a pointer would work in action and it does nothing when i run my code.


In my opinion this is a terible way to see how a pointer works. As far as I know (please correct me if I'm wrong) stepping through an array is the only time you can increment a pointer like that and since there are much cleaner ways to step through an array you really should just not ever do it.
BlaDe16
BlaDe16
i changed the line to for(int* ArrayPointer = Array; ArrayPointer < Array+10; ++ArrayPointer) cout<<(*ArrayPointer);

first thing is that 10 shouldnt it how would i make it that if i changed int Array[10] to 15 that i dont have to change that +10 to +15, but rather something like + size would be on the line?

secondly can you steer me in the right direction to what i should try to be doing with my pointer? Right now its results gives me the exact same thing I could do without a pointer, sorry if this is an annoying question.

Every example ive seen is either to complex or it just tells me a pointer points to another variable...
Captain P
Captain P
There have been quite a few questions on pointers recently. Try searching for them, some of the regulars here have written pretty interesting and usefull replies in some of those threads.
CompEngRH
CompEngRH
Quote:

first thing is that 10 shouldnt it how would i make it that if i changed int Array[10] to 15 that i dont have to change that +10 to +15, but rather something like + size would be on the line?

Use a const int instead.
const int MAX_SIZE = 2;myArray[MAX_SIZE] = {0,1};


Quote:

secondly can you steer me in the right direction to what i should try to be doing with my pointer? Right now its results gives me the exact same thing I could do without a pointer, sorry if this is an annoying question.


Are you just trying to understand what a pointer does? Try this http://en.wikipedia.org/wiki/Pointer

rip-off
rip-off
If you are dealing with a local array, the following template function will give you its size:
template<class T, unsigned N>unsigned array_size( T (&array)[N] ) { return N; }int main(){   // we can actually omit the size of the array   // the compiler will figure out what size to give it   // from the number of elements on the right   int Array[] = {0,1,2,3,4,5,6,7,8,9};   for(int* ArrayPointer = Array; ArrayPointer < Array + array_size(Array) ; ++ArrayPointer) {      cout << (*ArrayPointer) << ' ';}


It works because an array type includes two pieces of information, the type of the elements in the array and the size of the array. We use the template (with some rather funky C++ syntax for passing an array by reference to avoid pointer decay) to extract both the type and the size, although we only care about the size.

While this example may seem trivial, consider that the loop structure above is similar to most of the Standard C++ Library algorithms that take a range. For example, we can use the algorithm std::copy to get the same effect:
#include <iostream>#include <algorithm>#include <iterator>template<class T, unsigned N>unsigned array_size( T (&array)[N] ) { return N; }int main(){   // we can actually omit the size of the array   // the compiler will figure out what size to give it   // from the number of elements on the right   int array[] = {0,1,2,3,4,5,6,7,8,9};   std::copy(array,array + array_size(array), std::ostream_iterator<int>(std::cout," "));}
BlaDe16
BlaDe16
In computer science, a pointer is a programming language data type whose value refers directly to (or “points to”) another value stored elsewhere in the computer memory using its address. Obtaining the value to which a pointer refers is called dereferencing the pointer. A pointer is a simple implementation of the general reference data type (although it is quite different from the facility referred to as a reference in C++).

I think "using its address" above means the pointers address not the object its pointing ats address Other than that I get exactly what a pointer is, from that definition and others that are similiar, i guess ill have to think about this more, thanks for the help.

BlaDe16
BlaDe16
thanks rip off, that template is very interesting, now to figure out exactly what its doing...
gregrampage
gregrampage
Quote:
Original post by BlaDe16
In computer science, a pointer is a programming language data type whose value refers directly to (or “points to”) another value stored elsewhere in the computer memory using its address. Obtaining the value to which a pointer refers is called dereferencing the pointer. A pointer is a simple implementation of the general reference data type (although it is quite different from the facility referred to as a reference in C++).

I think "using its address" above means the pointers address not the object its pointing ats address Other than that I get exactly what a pointer is, from that definition and others that are similiar, i guess ill have to think about this more, thanks for the help.


No that's backwards. The pointer uses the address of the object it's pointing to. Otherwise how would it know where to point?
BlaDe16
BlaDe16
i tested it and i see what you mean, thanks for clearing that up for me.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.