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.
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. constchar *text = "My Text!";, makes text a pointer to 'M'. constchar(&text)[9] = "My Text!"; or auto& text = "My Text!"; makes text a reference to the actual array.
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.
@ 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?
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.
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.
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 :)
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?