pointers

is pointers really important? i mean there are some people saying that it will not be useful in future
closed account (SECMoG1T)
To my knowledge they are very important, you need understand them
Pointers are extremely important in c++..If you don't like them you can learn languages that handle pointers like in java...They arnt that complicated though...they just look intimidating because of the *..All they do is store the address of where a variable is located in ram...and you can access that variable's value by what is called de(reference)ing a pointer using a *...if its just a regular pointer *ptr.

You have to understand the context of when using the *..When declaring and when accessing the value stored at the pointer's memory location

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int x = 5;
int* ptr;  // int* means declare a pointer of type int
              // this is needed because the computer needs to know how big 
             // the dataType is going to be for the pointer
             // int is 4 bytes 
             // double is 8
             // string 24...ect
ptr = &x;  // using the & (address of) operator here
              //pass the memory location(address) of x into ptr

cout << ptr << endl; // output the memory location of x
cout << *ptr << endl; //dereference the pointer to x and output the value

//ptr = &x
//*ptr = x
Last edited on
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
38
    //Memory location starts at 100
    
    //============================================
    // 100   int x = 5;<---- stored at 100        <-+
    //============================================  |
    // 101                                          |
    //============================================  |
    // 102                                          |
    //============================================  |
    // 103   end of x                               |
    //============================================  |
    // 104  another variable location               |
    //============================================  |
    // 105                                          |
    //============================================  |
    //                                              |
    //                                              |
    //  somewhere else in ram                       |
    //============================================  |
    // 500 int* pointer_To_X = &x;      -->--->--->-+
    //============================================
    // 501
    //============================================
    // 502
    //============================================
    // 503
    //============================================
    // 504
    //============================================
    // 505
    //============================================
    // 506
    //============================================
    // 507 ---end of pointer_To_X
    //============================================
    // 508
    //============================================
    
Last edited on
Understanding them is both important and useful. The latest language version and standard library offer syntactic sugar that hides raw pointers, so you don't need to use as much pointers directly as you did with early C++.

For example:
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <algorithm>
#include <vector>

int main () {
  std::vector<int> myints {3,7,2,5,6,4,9};
  std::cout << "The smallest element is "
            << *std::min_element( std::begin(myints), std::end(myints) ) << '\n';
  return 0;
}

Did that use pointers?
Apparently not. Iterators are (usually) more complex objects than plain pointers.

Seriously, were there no pointers, loops, ifs, etc in that program?
There most probably were, but they are all hidden in the library object implementations.


Have you heard about polymorphism and class hierarchies yet?
When you do, you will learn that pointers -- at least smart pointers -- are invaluable.
Topic archived. No new replies allowed.