I have 2 questions about pointers in c.
1) from my understanding the &
returns the memory address of a variable. For example:
int x=10;
int *p=&x;
So I think that the &
will return the memory address of x
and that memory address is type int*
because the variable type of x
is int
.
Also because the type of the address is int*
I believe that is the reason that only an int*
(and void *
) pointer can point in the address (they are the same type).
Any thoughts about that? comments? I don't know if I am correct.
2) It's about the void pointer. I know that the void*
pointer can point to any type of variable. For example
int x=10;
void *ptr=&x;
Now I have a function:
void * foo(some parameters)
{
// just for this example let's say that I return the address of a local variable
// I know that is wrong to do so because when the foo ends
// the local variables deallocate
int k=10;
void *ptr=&k;
return ptr;
}
So my pointer is type void
but it points to int*
memory address. So ptr
will save the address of k
and when I return ptr
the address of k
is returned which is type int*
. But my function is type void*
.
What is happening here?