-3

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?

Stidgeon
  • 2,673
  • 8
  • 20
  • 28
Zeroslade
  • 31
  • 4
  • 3
    Please edit your question and fix code formatting and indention. – Lundin Jan 17 '18 at 10:13
  • 2
    Please ask only one question at the time. – user694733 Jan 17 '18 at 10:14
  • The pointer returned by the function you show will not be valid, as it points to a local variable inside the function, and that variables life-time ends when the function ends. – Some programmer dude Jan 17 '18 at 10:16
  • Somewhat of topic, but returning the pointer to a local variable is a bad idea, read [this](https://stackoverflow.com/questions/4824342/returning-a-local-variable-from-function-in-c) – Jabberwocky Jan 17 '18 at 10:21

3 Answers3

2

According to the C Standard (6.3.2.3 Pointers)

1 A pointer to void may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.

So for example in this code snippet

int x = 10;
void *p = &x;

the expression &x in the right side of the assignment has the type int *. According to the quote it may be converted to pointer of the type void * in the left side of the assignment.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

1) Yes all of that is correct.

2) The type of the pointed-at data remains int no matter what kind of pointer that points at it. (This is known as effective type in formal C.) In your example you return a void pointer so that's the type the caller gets. The information of what type it pointed at is lost, from the point you do void *ptr=&k; and onward. This is why void pointers are problematic to use.

Lundin
  • 195,001
  • 40
  • 254
  • 396
0

1) You are correct 2) It is simply returning a void * that is pointing to k - a local variable. (Any pointers can be cast to void pointers).

You are correct in saying that you should not use the returned value as k no longer exists

Ed Heal
  • 59,252
  • 17
  • 87
  • 127