+ 8

I cant seem to understand pointers. Can someone help?

I'm going through this in the C++ course, but my head can't understand this at all! Can someone explain how to use them, what they're for, and some examples also. I will really appreciate it because this question is often in my challenges and I can't understand this. //Thank you

9th Oct 2017, 7:28 PM
DeltaTick
DeltaTick - avatar
10 Réponses
9th Oct 2017, 7:40 PM
Babak
Babak - avatar
+ 12
i like question!
9th Oct 2017, 10:50 PM
FIREmonger
FIREmonger - avatar
+ 6
It's OK to be confused by pointers at first. It's just one of these topics that is hard to grasp for beginners, but over time you'll master them. To understand a pointer, you need to understand what memory and address is. A computer memory is like a huge array, that contains all the programs data. An address is like an index in that array. By declaring a variable, you're assigning a name to some place in memory: int variable; // now some place in memory is called 'variable', and it can contain an integer The address of a variable may be obtained using operator &: cout << &variable; // will print a memory address of variable (pretty useless number) You may declare a pointer to a variable of some type, using *: int *pointerToInteger; // Pointer to an integer. Its currently uninitialized You may assign an address to a pointer, making it "point" to the value, located at that address: pointerToInteger = &variable; // get the address of 'variable' and assign it to our pointer Now our pointer points to the memory location, that we called 'variable' at first step. This efficiently makes these statements access the same memory location: cout << variable; cout << *pointerToInteger; Oh yeah, to get the value, the pointer points to, * operator is used: cout << *pointerToInteger; // will print the value it points to cout << pointerToInteger; // will print the address it holds, same as &variable
9th Oct 2017, 7:47 PM
deFault
+ 6
Thanks!!!
9th Oct 2017, 7:47 PM
DeltaTick
DeltaTick - avatar
+ 4
simple example is.. pointer contains your variable address, other hand you can say, pointer point your variable...
9th Oct 2017, 8:09 PM
meherDev
meherDev - avatar
+ 3
To get better understanding, experiment with pointers yourself. Try to implement some data structures. Creating linked list is a good exercise for beginners (it requires basic knowledge of classes or structures too). Just remember, accessing an uninitialized pointer, or pointer that holds a meaningless address, may crash your program or result in ANY unpredictable outcome. Accessing pointer initialized to zero — most definitely will crash your program (zero or NULL or nullptr is usually used as a forbidden address, all pointers, not holding a valid address should be initialized to 0, that way you can at least check if it is valid).
9th Oct 2017, 8:05 PM
deFault
+ 2
Pointer is a variable which contains the address in memory location of another variable... we can have a pointer to any variable. To declare pointer to a variable: int * pointer; Also we must associate a pointer to a particular type...you can't assign the address of a short int to a long int ,for instance. Advantages of pointer are:- a) pointers are more efficient in handling array and structure. b)it reduces length of program and it's execution time c)it allows c to support dynamic memory management d)pointer allows reference to function and thereby helps in passing of functions as arguments to other function.
10th Oct 2017, 11:22 AM
Prabhat Thakur
Prabhat Thakur - avatar
+ 2
use pointers to hold reference to objects.
29th Oct 2017, 11:38 PM
Gao Xiangshuai
Gao Xiangshuai - avatar