Pointers

Well I just started learning pointers and I am officially confused...mainly when allocating new variables. Can someone suggest some tricks to learning how to implement these? Seriously, how often are pointers used in the industry? Why the need for so much indirection? I dont see why they are so important...

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <iomanip>

/* This program is emphasizing the difference of pointers and reference values
where
&p
p
*p
&x
x
all have different but interrelated values. */

using namespace std;

class classExample
{
public:
	void setX(int);
	void print() const;
private:
	int x;
};

int main()
{
	int xxx;
	int *p;
	int x = 37;
	cout<<"x = "<<x<<endl; // x = 37
	p = &x;
	cout<<"*p = "<<*p<<",  x = "<<x<<endl; // *p = 37, x = 37
	*p = 58;
	cout<<"*p = "<<*p<<",  x = "<<x<<endl; // *p = 58, x = 58
	cout<<"Address of p = "<<&p<<endl; // &p = 006BFDF4
	cout<<"Value of p = "<<p<<endl; //  p = 006BFDF0
	cout<<"*p = "<<*p<<endl; // *p = 58
	cout<<"Address of x = "<<&x<<endl; // &x = 006BFDF0
	cout<<"Value of x = "<<x<<endl; // x = 58
	cout<<endl;

	classExample *pointer, value;
	pointer = &value;
	pointer->setX(30);
	pointer->print();
	value.setX(50);   // this set x statement changes both values of the class variable
	value.print();    // *pointer and value
	pointer->print();

	int *intList;
	int arraySize;
	cout<<"\nEnter array size: ";
	cin>>arraySize;
	cout<<endl;
	intList = new int[arraySize];  // what is exactly going on here from *intList to intList??
	for(int i = 0; i < arraySize; i++)
		intList[i] = 0;
	for(int z = 0; z < arraySize; z++)
		intList[z] = z * 5 + 5;
	cout<<endl;
	static int num1 = 0;
	for(int u = 0; u < arraySize; u++)
	{
		cout<<setw(5)<<intList[u];
		num1++;
		if(num1 % 5 == 0) // formatting
			cout<<endl;
	}
	cin>>xxx; // read output
	return 0;
}

void classExample::setX(int num1)
{
	x = num1;
}

void classExample::print() const
{
	cout<<"x = "<<x<<endl;
}
Pointers are used all the time in the industry.

For now, since you're a beginner, the most relevant use of pointers is when you need
to store an arbitrary number of elements and you don't know how many there will
be until run time.

For example, if you wanted to implement a grade book program you won't know at
compile time how big to make an array of students because you don't know how many
students are in the class. So you can't just make a fixed-size array; you have to
dynamically allocate an array, which involves pointers.

In an effort to master C++; couldn't I just use a class that had an array of say 200 elements at the most? Just initialize the unused compinents to a default value? I understand because I'm a beginner that there may be millions of uses that I don't understand yet. I mean, memory is of little concern these days. I don't know...wouldn't it be possible to do the same thing without pointers and just use reference parameters and values? What I don't get is when to use a pointer versus reference. Aren't they the same? I'm not trying to be difficult, I just want to master what is going on here.
what if u want to save data for more students than ur stack can hold?...
you will not be able to store other vars the easy way on the heap, without pointers...

and references and pointers are not the same... u dont need to dereference a reference :P...

Because references always “point” to valid objects, and can never be pointed to deallocated memory, references are safer to use than pointers. If a task can be solved with either a reference or a pointer, the reference should generally be preferred. Pointers should generally only be used in situations where references are not sufficient (such as dynamically allocating memory).
Well, realisticly, what kind of class has more than 200 students? I'm starting to get the hang of it though..it was really confusing at first. But honestly, wouldnt't this be part of requirements engineering and elicitation? You know, making the coding process easier and less complicated? And I still don't get how the dereferencing operator works in conjunction with variables fully.... i.e.

1
2
3
4
5
6
7
8
int **board    // pointer to a pointer???  WTF?
int rows, columns;

cin>>rows>>columns;

board = new int* [rows];
for(int row = 0; row < rows; row++)
board[row] = new int columns;


hmmmm...maybe I just need to practice it a lot, this is way confusing.....and the next chapter is overloading and templates....yeah right....
And I still don't get how the dereferencing operator works in conjunction with variables fully

did u try the documentation on this page?...
Well, realisticly, what kind of class has more than 200 students?


An online course easily could.

Avoid assuming an absolute maximum.
closed account (S6k9GNh0)
Agreed. You must always expect the unexpected, literally. Those are where bugs form the most.
Okay, I think I have this down...is it safe for me, semantically, to relate a pointer variable to an address instead of an actual variable? Just like the variable inside the array subscript operator? Because a pointer can manipulate any address..the value located in the address, assigned to it? That's the emphasis of pointers no? Allocating dynamic memory?
Topic archived. No new replies allowed.