I'm having trouble with pointers... can someone explain the concept very well? lol
Printable View
I'm having trouble with pointers... can someone explain the concept very well? lol
Pointers are variable that store addresses, simply put. Perhaps this analogy can help you:
http://cboard.cprogramming.com/showp...3&postcount=31
And the tutorials on the site can maybe help too.
A pointer is a variable that holds the address of something. Let's say we have an int variable - that variable can hold an integer value, say 1234. The value held in a pointer is not a value that makes sense to us humans in the same way that the 1234 does. Instead, it holds the address of a piece of memory that holds the actual value. So, for example, we can have this code:
Now px will hold the address of the variable x. If we use the dereference the pointer:Code:int x = 1234;
int *px = &x;
then we will get the value of x, 1234.Code:printf("%d\n", *px);
If we change x at some later point:
then the printf above will show 4711.Code:x = 4711;
We can also change px itself:
This would make the pointer point to y instead.Code:// add this at the top of the code.
int y = 1811;
px = &y;
--
Mats
Probably not to a person who ends questions with lol, no. Have you searched the forums here for any of the many times people have tried?
The brief version: a pointer is a variable that stores a memory location. Just as "1000 Main Street" allows you to find the court house, "0xc3108252" allows the computer to find a piece of storage.
It is not like a memory bank. It is merely a variable holding an address.
It can hold the address to a memory bank, yes.
In the concept of dynamic memory, malloc reserves a memory block with at least the specified size and returns the address to where that is, and thus it needs to be stored in a - yes, you guess it, a pointer.
Think of it as like using a GPS receiver. The receiver doesn't contain the location on the map it shows. It just tells you where you are. Its like that, but instead of finding your position in the world. It finds your position in memory.
I would rather just think of pointers as addresses. It is all very well explained in the link I gave earlier. It is a very easy to understand analogy.
Ah yes. I love that post :) I just gathered that the OP wasn't totally getting it so I threw in my two cents. But yes, I do think that analogy was very well writen. Mine was just spur of the moment and superficial.
I got the feeling the OP had ignored the link or made no attempt to understand it.
lol seems like , it just looked at the maps post i suppose. He got exicted as soon he say we codes and stuff with the explaination!
@OP
- POINTERS ARE ADDRESS HOLDERS
- THEY HOLD ADDRESS OF A GIVEN VARIABLE]
ssharishCode:*p
_______
0x0018 |0x0025| <-- Pointer pointing at memory location 0x0025
|______
|
|
V int data; <-- variable name
________
0x0025 | 10 | <-- Variable located at memort location 0x0025 and holding value 10
So with the above structure, these are the things which you can do
p = & data ( You are initializing the pointer before you the following,
otherwise the pointer p is pointing no where!).
*p would get you 10 ( This is called as deferencing the pointer)
p would get you 0x0025 ( This gives you the address of where the pointer is pointing. )
&p would get you 0x0018 ( This would give you the address of the pointer).
You get special bonus points for ascii artistic skills, ssharish :)
I just got a ticket, but your ASCII art skills definitely improved my mood.
A pointer is the address of a variable, it's quite simple.
Thus to work with the variable you need to look at what is located at the address
rather than the address itself.