# I give up on pointers

Printable View

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 12-04-2002
devour89
I give up on pointers
I've read mutiplul tutorials on pointers and boobs ut i still don't understand them... can anyone help me?
• 12-04-2002
devour89
I meant books
• 12-04-2002
Lynux-Penguin
• 12-04-2002
master5001
I'll give this one a shot. A pointer is a number that is called a "memory address." This just means that it points to an area of memory. For instance:

Code:

```int i; int *j = &i;```
j points to the memory location in memory. i is a variable that is some area of memory that is used by the stack (the memory that your program is using). Note that rather than saying j = i I wrote j = &i. The and operator is used to get an address of a variable. Now lets say I want to set the memory that i takes up to 7.

Code:

`i = 7;`
Since j points to the same memory you could also do this:

Code:

`*j = 7;`
I used the * operator to dereference my pointer. Since j is a pointer saying j = 7 would mean that you wanted j to point to memory address 7. Dereferencing means that you want the memory address pointed to by the pointer to change rather than the actual pointer. You may want to re-read that sentence until you fully understand it.

Now lets get into some pointer math.

Code:

```int i[10]; int *j = i;```
Notice that since I is an array (technically a pointer) I am setting j equal to i rather than setting j equal to i's address (i's address would be the address of a pointer not the address of an int). Now here is some math.

Code:

`j += 2;`
Since j is a pointer the memory address that the pointer is pointing to is incremented not the value that the memory contains. Let me clarify. Lets say that j[0] = 7, j += 2 means that j now equals j[2] not j[0] = 9. Now do you understand?
• 12-04-2002
master5001
Re: I give up on pointers
Quote:

Originally posted by devour89
I've read mutiplul tutorials on pointers and boobs ut i still don't understand them... can anyone help me?
If I have a book that you want and I tell you where to get it would I not technically be a pointer to a book :)

(and no I don't have a book on pointers, but I do know they exist just look on amazon)
• 12-04-2002
Polymorphic OOP
Quote:

Originally posted by master5001
Notice that since I is an array (technically a pointer)
No, it's not a pointer at all, it's an array.
• 12-04-2002
boobs
i see
could someone tell me, when pointers are useful and stuff.
• 12-04-2002
MadHatter
>No, it's not a pointer at all, it's an array.

i thought it was about the same thing? you can use them in the same way.. e.g.

int a[10];
int *b = new int[10];

are pretty much the same.. except for that 'a' is a const pointer (i.e. you can't change what it points to)

*(a+3) = 7; //same as a[3] = 7
b[3] = 7; //same as *(b+3) = 7

so what's the different? 'a' is just an address of the first element (a[0]) same as 'b' is an address of b[0] (right?)

maybe i've got it all wrong.. oh well.
• 12-04-2002
OneStiffRod
I think the easiest explanation of pointers would be that it's like the SHORTCUT icons on your desktop. They simply hold the Address or Path to where the actual program is. Similarly, pointers are the address in memory where your variable resides - it's much easier and quicker to pass the shortcut or address of the object in mem than the object itself. When you pass by value you are making a copy of your object and passing it to the function - when you use a pointer or reference you are eliminating the copy process and are speeding up the process by passing an Address to the function which is often less bytes than the object itself.

Try out this code....

Code:

```#include <iostream> using namespace std; int main(){   int *ptrINT = 0;  //Here we have created a pointer and set it's value   cout<<ptrINT<<" here we see the value held in our pointer ptrINT, this should show us a hex value 0xhhh"<<endl;   cout<<*ptrINT<<" here we have dereferenced the pointer (*ptrINT) and it should show us the actual value contained in the memory address that the pointer points to. It should be a value of 0."<<endl; return 0; }```
• 12-04-2002
Polymorphic OOP
Quote:

Originally posted by MadHatter
i thought it was about the same thing?
No, they're not at all. An array is an array is an array! A pointer is a pointer because it's value is an address in memory. An array's value is an array of data -- its value is not a memory location at all!

The only thing about an array that has any relation to a pointer (and only through syntax not through implementation), is that when you use an array name by itself in an expression it represents a pointer to the array's first element.

The best analogy I can give is the return type of a function:

Code:

```int* MyFunction(); int MyArray[10]; int main() {     *MyFunction() = 5; // I'm using the return value of MyFunction                       // as a pointer to an int, but that doesn't                       // mean that the function itself is a pointer                       // to an int     *MyArray = 5; // Same concept here     return 0; }```
• 12-04-2002
Polymorphic OOP
OneStiffRod's explained one reason why to use pointers pretty well.

Another reason for using pointers is the fact that when you dereference a pointer you're using the actualy value of the original data. This is useful, especially for functions, where you want the function itself to alter the value of one of it's parameters.

For instance:

Code:

``` void SwapNumbers( int* Number1, int* Number2 ) {     int Temp;     Temp = *Number1;     *Number1 = *Number2;     *Number2 = *Temp; } int main() {     int a = 4,         b = 8;     SwapNumbers( &a, &b );     // Now b == 4 and a == 8     return 0; }```
This also is very essential when working with objects, though it's done internally. Let's say you have an object that represents a play -- if everytime you passed it to a function you were dealing with a copy, then anything you altered for the player would only affect that temporary copy (not to mention you'd waste time and memory making a duplicate).

Another reason for pointers is to be able to directly work with dynamically allocated memory, which is memory that you allocate at runtime.
• 12-04-2002
MadHatter
i guess it sort of depends on how you look at it.. an array is an array. but the identifier (the name you give the array), as i understand it, holds an address (the address of element 0), making it much like a pointer.

so if you say

int x[10];

'x' holds the address of x[0], and all the other elements are accessed by using subscripts or pointer-offset (i.e. *(x+2)).

bah.. oh well.. i'm going to bed :rolleyes:
• 12-04-2002
devour89
thanks for all the help!
From what i understood i made this:

Code:

```#include <iostream.h> int main() {  int x = 10;  int* pX;  pX = &x;  cout<<"Memory Adres of pX: "<<pX;  cout<<"\nPointer to 'x': "<<*pX;  cout<<"\n'x': "<<x;  cin.get();  return 0; }```
I understand it now but i still don't see the point in using them when you can do the same thing by just giving 'x' to a function. Why would you even bother to go into all that pointer stuff?
• 12-04-2002
Polymorphic OOP
Quote:

Originally posted by MadHatter
the identifier (the name you give the array), as i understand it, holds an address (the address of element 0)

...

'x' holds the address of x[0]

Nope, absolutely no pointer is created when you make an array. The identifier isn't a pointer, C++ just uses syntax that allows you to quickly get a pointer to the first element via its name. Internally it does exactly the same thing as &ArrayName[0]
• 12-04-2002
devour89
Well I'll take your words for it. Every tutorial I have read they say the same thing after you get used to them you won't progarm without.

I guess i better get used to them and start using them more often in my programs.

Thanks again for all the help Guys.

-Devouring One-
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last