Trying to understand pointers!

Hi!
New to C++ and pointers, trying to understand Why they exist. I understand how it works (I think) with them but it is the usage i dont understand.

http://www.cprogramming.com/tutorial/lesson6.html

That is the article i have been reading so i will use that example code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

using namespace std;

int main()
{
  int x;            // A normal integer
  int *p;           // A pointer to an integer

  p = &x;           // Read it, "assign the address of x to p"
  cin>> x;          // Put a value in x, we could also use *p here
  cin.ignore();
  cout<< *p <<"\n" << x; // Note the use of the * to get the value
  cin.get();
  
}


Okey so here we are declaring an interger and a pointer, then asign the memory address of variable x to pointer p. So everything i put in variable x will be accessable from pointer p.

I understand this but why? :)

Why not just use the variable x ? What is the advantage of doing this?

// Clint
See my posts about half way down this thread.

http://www.cplusplus.com/forum/beginner/60951/

I could summarise that thread as pointers are needed to make big arrays (or indeed, huge objects), to interact with hardware, to make arrays of size that you don't know at compile time, polymorphism (this is a big one), and efficient algorithms (think linked-lists etc).
Last edited on
Because passing a 4 byte (or 8 byte) pointer is much faster than passing a 500+ byte object. I'd say with modern programming, that's their main use. And I'm sure Moschops will correct me if I'm wrong.
I say your commecnts in the thread and i also found the tutorial on this website:

http://www.cplusplus.com/articles/EN3hAqkS/

This was the only concrete example on when a pointer is good to use.

1
2
3
4
5
  int a = 20002;
  char* p = (char*)&a; // Make a char*, and force the compiler to treat the address of a as a char pointer.
  for (int i =0;i<4;i++)
    {std::cout << (int)(*p) << std::endl; p++;} // Cycle through the 4 bytes of the int, outputting each byte as if it were an int
  return 0;


This will print the four bytes in the memory that holding my integer 20002:

34
78
0
0

34 is 0010 0010b
78 is 0100 1110b

0100 1110 0010 0010 = 20002!!!

This is so cool :D :)

@ResidentBiscuit
So you mean that if i want to pass a big array of something it copies the whole array, instead of just passing a pointer to it?
An array is already kind of passed as a pointer to the first element anyway. I'm talking about passing an object (instance of class).
Ok, With pointers. Pointers are great if you want to change the global variable from within a function. Then you pass the function the address and it can use the variable and makes its changes.

Pointers are a big part when dealing with hardware and drivers. They are not going to just give you Variable X to play with just some memory address :)

Another good use of pointers is Reverse Engineering. When Reverse Engineering you need to see what is put into memory to more or less see what the function you are dissecting is returning so you can figure out what it does.

Pointers are the corner stone of the C language and what makes it so powerful.

As ResidentBiscuit has said an array is stored in memory in sequence. So you can use pointer math to navigate your way through the array if you wish.

Those are just a few uses of why pointers are even needed. I have seen code where people use pointers just because they can and with no need so they make it way to complicated. On the other hand I have seen lots of code where pointers could have saved them a ton pain and agony because they set variable X = 1 and then modify it with a function and return it to variable Y then use Y later and never touch X again. C++ is very Object Oriented also in a way reducing the need to use pointers.
Pointers are a tool and like any tool can be misused. Very worth the effort of getting comfortable with.
C++ is very Object Oriented


C++ is very multi-paradigm. You don't need to OOP at all with it.
Wow you jumped right on that one. You are right, you don't need use any OO with it. Being "multi-paradigm" it is a functional,object-oriented, etc.. It comes down to the software designer to use it as they please.
Another use, to point.
Consider this scenario:
_ You buy a car. You drive the car to all places (you are the driver)
_ One day your wife 'ask' to borrow the car. Then she lends it to her lover.
_ Then it's struck by a fuel-truck. The man was drunk and made the vehicle flip, setting all on fire.
_ Your car kindly inform its driver that he's dead (the driver)
_ ...
_ Your body is found in your room. The doors and windows are closed. No wounds are visible, the blood test were negative.
_ A small kid with glasses was pointing out the weird circumstances.
Looking at your PC he discovered that you were learning C.
Looking at a weird stain in the wall, your wife affair is revealed.
That's all he needs to find the culprit...


Basically you have relationships between objects. Those relationships may change with time.
Last edited on
Thanks for all the help!

Though i understand how pointers works i think i need some more generic knowledge of C++ before i really understand how to use them in a efficient way :)
Topic archived. No new replies allowed.