+ 13
What are pointers?(Help me to summarise)
I went through the lesson of pointers and didn't quite get it what it is used for or how it is used. Can someone help to summarise it for me in simple english and teach me how to use it? Thanks!
17 Respostas
+ 21
pointers are the address holder, pointer store address of objects. want you what change value of something but you know only address of it.it mostly use in GUI.
example
int var = 20;
int *p = var;
we created a pointer named p assign the address of variable var; spouse the var has address 0x8f6f97, pointer p has now it's address (0x8f6f97) when ever you change the value of pointer using * you are going to change the value of variable whose address is stored in it;
*p = 50;
now var become 50.
hope you know some thing about it.
+ 7
Now that i've learn about pointers, the short answer is that a pointer variable holds a reference to another variable or a piece of memory. Using pointers you can access or change the data it points to, or its members if it points to an object. Using pointers (and only pointers) you can pass variables by reference to functions because in fact you pass the pointer, the reference to the variable and you can use that reference to modify the data it points to. Frankly it's a waaay to complicated method of doing things but then again c++ is pretty old.
Think of it like the shortcuts on your desktop, they point to the real file and through them you can open and modify the file, you can create more copies of the same shortcut but they all point to the same file on disk, if the file gets deleted the shortcuts are invalid.
+ 4
Ok Rizwan, thanks alot. I now understand it better.
+ 3
@Steve DICAY. Won't that assign the value of myNewVar to the variable *p points to ? This is the confusing bit. How does it differentiate between assigning a new value to the var it points to and changing the var it points to.
+ 3
hmm, I'm very curious who and why down voted my posts...
+ 3
A pointer only stores the address of a variable if the address-of operator is used in front of the variable.
int x = 45
int* p = &x
That holds the address of x as p. You can then access the value of x by using *p or just x.
+ 2
in short, pointer is a variable holding the address of some other variable... :)
+ 1
So a pointer references the variable (the memory address it was first assigned to it)? If assigning a value to the pointer will assign the value to the variable it references, how do you change the address the pointer references, meaning how do you point to another variable?
+ 1
@Eduard yes I even have -1 votes. and 0 votes where you put me a +1
+ 1
stop arguing over votes and downvoting people for stupid reason before I delete this question(since I got answer + I dont want this to become a arguing site)
+ 1
Pointers are variables that hold memory address of another variable of same data type.
int a=10; //stored at address 1028
int*p=&a; //& returns address of the variable
*p will now access value at address 1028. and simply displaying p will display the address of variable a, that is 1028.
hope im clear to you
0
@Eduard. why would you change the address of the pointer ? the computer is the one dealing with the allocation of adresses.
*p = myNewVar;
I guess to assign it to a new variable
0
yes sorry
This assign the address of myNewVar to p :
p = &myNewVar;
& is used to designate the address of a variable
0
K2 shape. where do you see arguing ? we just wondered where had our points gone. If you are not happy with people's answer you can delete your question. people have just answered to help you. So don't complain.
- 1
pointer point on the data type which may be int,float or something....it holds address of another variable...and it can also store value of that variable.....using * follow by pointer variable
- 1
Fun fact: In C/C++, arrays and pointers are almost identical, and are often used as such. That's because (at least for x86 asm) at the machine language level is calculating the address given the address of the first entry, each entries size, and what the index is. Say you have the array
unsigned int Image[IMAGE_WIDTH * IMAGE_HEIGHT];
and you want your computer to draw some purdy colours all over it, so you write a function:
void SetPixel(unsigned int X, unsigned int Y, unsigned int Colour)
{
Image[Y * IMAGE_WIDTH + X] = Colour;
}
This is read by the processor as:
*(Image + sizeof(unsigned int) * (Y * IMAGE_WIDTH + X)) = Colour;
If you're curious, try replacing the body of SetPixel with the line above, you'll see they're functionally identical.
But yeah, pointers are exceptionally useful when dealing with passing around large blocks of data efficiently. They prevent a ridiculous amount of data copying.
- 1
how to draw syntax diagram declaring flot and integer .i attended lecture but i got nothing how to draw it.i need help