Uses for Pointers

Pages: 123
If that makes int p[10000000] then I would do p[1] = 4;


What do you think p[1] means? It's the same as this; *(p+1). Oh look. Dereferencing a POINTER.

You use pointers all the time and you don't even know it, so I'm not surprised you don't know what they're for.




1
2
3
int * y;
*y = 1232;
std::cout << *y


It returns the value stored at adress 1232?

No, that will cause a segFault (or, if you're unlucky, trash data without you knowing) because you have just written the value 1232 into the memory pointed to by y, which is some random memory value.

I suspect you meant this:
1
2
3
int * y;
y = (int*)1232;
std::cout << *y

which is very, very different and may still cause a segFault if the operating system doesn't think the memory location 1232 is something you should be reading.
Last edited on
Arrays are pointers to data initialized on the stack.

String literals are pointers to const char arrays:
1
2
//guess what this does:
cout << "Hello, World!"+3 << endl;
lo, World!

Ever wondered why you use this?
const char *text = "My Text!";,
           ^That's a pointer :p
Last edited on
LB wrote:
Arrays are pointers

shame on you for perpetuating a myth
shame on you for perpetuating a myth

Shame on you for not doing anything about it.
This would leads the topic astray, but sure: despite the strangely common misconception, arrays and pointers are completely different things.

An array T[N] is a fixed-size random-accessible container which stores N objects of type T, placed in contiguous memory locations (one after another).

A pointer to data T* is an object that maybe holds a reference to one T object, which (if it exists), exists elsewhere.

The confusion comes from the implicit conversion that C++ inherited from C, and C from B: arrays may be converted to pointers to their first elements. This is one of the many implicit conversions: char can be converted to int, int can be converted to float, function can be converted to pointer to function, reference can be converted to value, etc. That a conversion from T1 to T2 exists, doesn't mean that T1 and T2 are the same.

"My Text!" is an array of nine const chars. const char *text = "My Text!";, makes text a pointer to 'M'. const char(&text)[9] = "My Text!"; or auto& text = "My Text!"; makes text a reference to the actual array.
strangely common misconception


It's not really strange. As you say, the conversion is implicit, and when you can use two different things in the same way, thinking they're the same thing is pretty understandable.
Last edited on
Oops. I have learned. Thanks Cubbi
@ LB: I know the example would have been better using references, but the OP seems to know nothing about pointers or how they work at all, so I was just giving an example of how they could be used / showing the difference between sending a variable in by value or address.
The idea here is to present an example which cannot in any way be countered with "But why not just do ____ like this? ________"

Would you want to learn how to use something when you thought that in all cases there was an easier way to do just the same thing? I wouldn't in most cases, so until I was presented with something that could not be done any other way, I would not spend time learning how to use it.

@OP: Say you're making a game. How would you create all the game objects that you load from a file? Do the things you use use pointers?
Last edited on
I think this thread has been done to death. Numerous things have been presented that can only be done with pointers. The OP himself/herself used pointers for their own examples of why pointers weren't needed.
@ Moschops
Exactly, I know when they are NOT needed but not how or why they would be needed.
For example LB said:
Ever wondered why you use this?
const char *text = "My Text!";,
^That's a pointer :p

I've never done that, I usually do: string text = "My Text!";
Last edited on
Last edited on
BrokenSilenceDev wrote:
I've never done that, I usually do: string text = "My Text!";
Now, do you know how the string class does it? I'll give you a hint: it has an internal pointer to the string. ;)
LB wrote:
Now, do you know how the string class does it? I'll give you a hint: it has an internal pointer to the string.

Typical implementation of std::string holds an array of char directly (not a pointer) for short strings, and pointer to char plus two size_t's for long strings.

The OP himself/herself used pointers for their own examples of why pointers weren't needed.


Actually, he misused pointers.
Actually, he misused pointers.


p[1] = 4; doesn't really seem like misuse. :)


I'll give you a hint: it has an internal pointer to the string. ;)

Bingo. There's another pointer the OP is using without even realising it. The string class hides the mechanics of it, but its there.

I've never done that, I usually do...

Are we to take it also that you have never needed to create a large object, access hardware, control object lifetime yourself, polymorphism etc etc and all the other things we've stated? You might as well be coding in BASIC :)
Last edited on
Are we to take it also that you have never needed to create a large object, access hardware, control object lifetime yourself, polymorphism etc etc and all the other things we've stated?
Yes and no. Define large object.
Last edited on
Topic archived. No new replies allowed.
Pages: 123