Uhhh I got a question..... I never really got the idea of pointers, could some one tell me the real purpose? to save space? ahhhhhhhh.... I mean I know the code and all but I need to know the true concept of it.....
Printable View
Uhhh I got a question..... I never really got the idea of pointers, could some one tell me the real purpose? to save space? ahhhhhhhh.... I mean I know the code and all but I need to know the true concept of it.....
In C, to pass something by reference, instead by value. However in C++ we have 'references' to do this.
But this is not the only use of pointers in C++(Hint: polymorphism).
here is a simple, sample of a int pointer in c++6 console.
To add, pointers are used in the dynamic memory allocations.
pointers make life harder for programmers.
pointers represent indirection, meaning you have a variable storing the memory address of the actual variable, the name says it: they're not the variables themselves, they point to the variable.
Right, but what is the advantage of knowing and pointing to a variable? Who cares whats the location of the variable? its all FFA84kdhd yada yada yada, what is it good for?
nobody actually cares about the addresses, thats why its so bloody stupid and makes life a pain for programmers. In other languages all variables are indirectly referenced anyway, so the actual advantage in C/C++ is that variables don't have to be indirectly referenced, which saves you loads of performance, but when doing anything else pointers are just lowlevel pain.
heres a classic use. Consider this definition:
Now try to compile this :DCode:template <class sometype>
struct NODE
{
sometype value;
NODE<sometype> *next;
};
int main()
{
NODE<int> x;
return 0;
}
Code:template <class sometype>
struct NODE
{
sometype value;
NODE<sometype> next;
};
int main()
{
NODE<int> x;
return 0;
}