# Thread: Understanding Pointers - Easy Questions.

1. ## Understanding Pointers - Easy Questions.

I'm having so much trouble grasping pointers. I'm experimenting with pointers and I expect to ask several questions on this thread. My first confusion is notation.

I've gotten over declarations at this point. If I do

Code:
```int main ()

{
int *ptr;
}```
ptr is a pointer. But I'm confused about using it in code. When I use ptr, there are times when I don't include the star and times when I do. How can I know when to use it?

for instance, given that *numPtr is declared:

*numPtr = malloc(sizeof(int));

does not work, but

numPtr = malloc(sizeof(int));

does work. So here I need to use the star char.

But when I use the equal operator, I cannot use the star symbol. Why not?

PtrPtr = numPtr (correct)

printf("%i\n", *PtrPtr); (requires a star, won't work without it. Why not?)

2. Just remember "declaration follows usage".

If you have the declaration
Code:
`int *ptr;`
in the usage ptr, it is a pointer to int; the usage *ptr, it is an int;

So, having as in your example: int *numPtr; we need

Code:
```numPtr = malloc(sizeof (int)); /* numPtr is a pointer, which is right for the return value of malloc() */
PtrPtr = numPtr; /* numPtr is a pointer, which is the same type as PtrPtr */
*numPtr = 42; /* assign the int 42 to a memory location that can hold ints */
printf("%i\n", *numPtr); /* *numPtr is an int */```

3. Let's work with the following declaration
Code:
```int x = 6;
in* p;```
p is a pointer.It is ready to point to variable of type int.
In your computer, memory is allocated so that there an address can be stored.
When you say
Code:
`p=&x;`
you assign the address of the variable x to the memory that was allocated for the pointer.
Now how to read the operators * and & for a random variable a:
Code:
```&a = the memory address where a is stored
*a = the content of the memory address that a is pointing to```
When we want to set a pointer to point in the same place another pointer is pointing to, we have to copy the address of the "direction" we want our pointer to get.
So we do this
Code:
```/* Declarations */
int x = 6;
int* p;
int* d;

/* Set p to x */
p = &x;

/* Now set d to point where p is pointing to */
d = p;```
malloc returns a pointer, that's why you have to use it like this

4. malloc returns a pointer, that's why you have to use it like this
That's not the reason.

Anyway I have this to add: If a pointer points to memory not returned by malloc/realloc/calloc, you can't call free().