Question about pointers and references

in the pointers tutorial on this site, pointers are shown being linked to references;

in alot of more advanced code, references (&someVar) are rarely used, while pointers are often used. the (*someVar) in the tutorial is called the Dereference operator. but take for example a DirectX framework; it often sets most of its variables with this dereference operator;

understanding this points directly to the data; i'm struggling to understand the difference between a variable and a pointer variable. int a = 5; and int * b = a; both are 5.

why all this subterfuge when referring to a value? why is it necessary and why are pointers so often used, outside of information hiding?
int a = 5; and int * b = a; both are 5.

This is not true.

Pointers hold addresses, not values. a is a value.

To access the address of a variable, we use the ampersand sign. We can then assign that address to a pointer which points to the same type as the variable, like so:

1
2
3
4
5
int a = 5; // 'a' holds the integer 5
cout << a; // prints 5

int* b = &a; // b is a pointer that holds the address of a
cout << b; // prints address of 'a' in hexadecimal form.  
Last edited on
Hi,

why all this subterfuge when referring to a value? why is it necessary and why are pointers so often used, outside of information hiding?


With C++, prefer to use references rather than pointers, unless they are required for some library. A pointer can be made to point at anything, including nothing, while a reference cannot be re-seated - it must refer to a variable.

It matters when the object is something potentially large - it's more efficient to pass the address of the variable (4 bytes) as opposed to copying the whole 1MB object. So if it is an STL container like std::string or std::vector or it's a class (maybe your own) pass by reference.

It isn't usually worth it for built-in types like int or double, unless one wants to change the value of it in an outer scope. Changing a variable's value in an outer scope is handy for changing multiple values - one can only return one value from a function, so pass several variables by reference and alter them inside the function.

Pointers are used for other things - like polymorphism, but another thing called smart pointers are preferred for this.

Raw pointers aren't usually needed in modern C++, but they might well have been widely used in various libraries or frameworks. Some of that might be to do with C++ being a federation of various paradigms - such as the C language. Libraries might have been written using C, but still can be used by C++.

Good Luck !!
@ArslanSenki

@TheIdeasBruv

say there is a type or class found in a library, they want you to create a pointer object of that type or class; for example:

SDL_WindowObject* window;

are you saying that this doesn't hold the object, but the address the object occupies?

say i have a Class,

Class datBoi { string _output = "dere goes dat boi"; string expect_Input = "OH SHEIT, WADDUP"; }

what is it doing when i make an object of this class as a pointer?

datBoi* db;

also, i've seen functions be intialized as objects (im from C# so i maybe not understand what happens with these declarations exactly)

extern void SomeFunction(char c, int d, <vector> v);

so i make an object of it:

SomeFunction* sf;

what is the point of actually pointing to these things? is it just like TheIDeasMan says, its just to directly access the type, function, class outside of scope? and why is this seem to be in lots of audio/visual libraries, or rather, what are some (basic) advantages?

I just want to know what i'm doing. so many pointers, its confusing to wonder what is actually going on.

thanks for the help
> why all this subterfuge when referring to a value? why is it necessary and why are pointers so often used,

See: http://www.cplusplus.com/forum/general/114879/#msg627171
Topic archived. No new replies allowed.