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...
#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. */
usingnamespace 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 = newint[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;
staticint 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;
}
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 = newint* [rows];
for(int row = 0; row < rows; row++)
board[row] = newint 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....
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?