+ 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
10 Réponses
+ 13
Two easy to understand tutorials for you
[https://www.tutorialspoint.com/cplusplus/cpp_pointers.htm]
[https://www.programiz.com/cpp-programming/pointers]
+ 12
i like question!
+ 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
+ 6
Thanks!!!
+ 4
simple example is..
pointer contains your variable address, other hand you can say, pointer point your variable...
+ 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).
+ 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.
+ 2
use pointers to hold reference to objects.