Pointers

closed account (iAk3T05o)
I don't really understand pointers. In fact, why should they be used?
I know the * means 'value pointed by ' and & means 'address of'.
1
2
3
4
5
6
int x = 0;
int *p_pointer;

p_pointer = &x; //value of p_pointer = value stored at address of int x

std::cout << *p_pointer; //will print 0 

that's like all i can do with and understand when using pointers.
Could you use it a program and explain? What's the usefulness of pointers?
This is a comprehensive explanation of pointers in C, it's enough to get you started. C++ has an added aspect subtyped polymorphism.
http://computer.howstuffworks.com/c20.htm
Nowadays I would say pointers are more useful in C than in C++.

>> In C, pointers can be used for performance, to prevent copying of large amounts of data.
<< In C++, there are references for this purpose.
http://www.icce.rug.nl/documents/cplusplus/cplusplus03.html#l36

>> In C pointers to functions can help improve the code.
<< In C++ this functionality is of dubious benefit. C++11 also adds std::function and lambda functions.

>> In C, pointers are used as dynamic arrays.
<< In C++, using dumb pointers for dynamic memory allocation is generally a bad idea (C++ exceptions are one reason why). For such purposes, C++ offers smart pointers such as std::unique_ptr and containers such as std::vector.

<< The other use of pointers in C++ is, as kbw mentioned, polymorphism.
Still, you could and should use smart pointers for this as well.

Edit: "Nowadays I would say pointers are more useful in C than in C++."
I mean that in the sense that modern C++ offers many facilities as an alternative to dumb pointers. One will still find a good use for dumb pointers in "serious" coding, when in pursuit of efficiency and performance.
Last edited on
closed account (iAk3T05o)
@kbw: thanks a lot. I think i get it but i'll need practice and i have no idea why C has so many % (i'm sticking with c++. I wish google could differentiate them).

@UK_Marine: i can't watch any videos now. I'm using a phone.

@catfish: i have no idea what you mean by lambada, polymorphism or smart pointers. I'm using this site's pdf + jumping into c++ to learn (i'll learn them when i get there).
I do know vectors
Nathan2222 wrote:
@catfish: i have no idea what you mean by lambada, polymorphism or smart pointers.


Lambda, not lambada.

Lambda functions are special nameless functions introduced in C++11 (the C++ standard of year 2011).

They work very well with functions that accept another function as a parameter.
For example std::transform() from the algorithm library accepts a function pointer, or a functor, or a lambda function as op or binary_op.

http://www.cplusplus.com/reference/algorithm/transform/

Polymorphism is when a pointer to a Base class can behave like a pointer of a Derived class.
You'll eventually get there by reading the FAQ but if you want to peek:
http://www.cplusplus.com/doc/tutorial/polymorphism/

Smart pointers are designed to handle dynamic memory allocation for you (more specifically, to automatically deallocate the dynamic memory). By using them you don't have to worry so much about memory leaks.

In C++98, there was std::auto_ptr.
In C++11, std::auto_ptr was deprecated and replaced by std::unique_ptr and a few others.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// somewhat bad C++98 code

int *pi = new int[10];
// ...
delete[] pi; // if this isn't reached, a memory leak happens

// somewhat better C++98 code

#include <memory>
// ...
std::auto_ptr<int> spi(new int[10]);
// deletes automatically

// modern C++11 code

#include <memory>
// ...
std::unique_ptr<int> spi(new int[10]);
// deletes automatically 

Topic archived. No new replies allowed.