+ 1
Pointers cpp
I dont Understand the pointers pleas explain it to me
5 Réponses
+ 1
Ouladtahar Abdelkader in regards to what they are used for:
The most obvious one is arrays (including strings). Array is just a pointer to the first element, and indexing is pointer arithmetic. Simillarly if you are declaring arrays on heap, u would be using pointers.
It's also often used to pass into functions as additional return values (return values to variables). Speaking of functions, there are also function pointers (which point to functions). Function pointers is what's used in C to create OOP. In C++ though it still has other uses.
There are a lot of other less intuitive pointer applications though, like reversing strings and such.
+ 2
Pointers are variables that store addresses. Just like the mail man knows your address is located where exactly. Pointers keep memory addresses to where some things are located.
& this operator gives you the address of something (eg 1000 main street, antarctica)
type * declaration is the pointer type
so
type * ptr = & me ; // stores my adress in ptr
if i want to get the address:
cout << ptr;
if i want the value of what that mailbox contains:
cout << *ptr;
in this last case, the * operator is called dereferencing operator
+ 2
"Thanks bro for the clarifications
No what are they used for"
If you are a complete beginner, try not use them at all for now.
It will come a time when you have to use pointers, likely when you implement some datastructures in your learning journey. in some datastructures, some elements of the structure may need to keep the address of other children, sibblings or parent elements.
0
Thanks bro for the clarifications
No what are they used for
0
Thanksssssss brothers