-2

Sorry for this Noobie question, I just can't make either of the following code work?

snippet 1:

int main(void) {
    void *ptr;
    *ptr = 1;
    printf("%d", *ptr);
    return 0;
}

snippet 2:

int main(void) {
    void *ptr;
    int a = 1;
    ptr = &a;
    printf("%d", *ptr);
    return 0;
}

snippet 3:

int main(void) {
    void *ptr;
    int a = 1;
    (int*)ptr = &a;
    printf("%d", *ptr);
    return 0;
}

any idea?

mko
  • 21,334
  • 49
  • 130
  • 191

4 Answers4

1

Snippet 1
You cannot de-reference a void pointer.
Snippet 2
Same mistake.
Snippet 3
*(int*)ptr = &a; is not needed. Void pointer can hold the address of any kind of pointer.

If you want to use void pointers you can do as below
void *p;
int a=5;
int *i_ptr = NULL;
p = &a
i_ptr = p
printf("\n %d",*i_ptr);

Here you are copying the address from the void pointer to an integert pointer. But you have to be sure that p contains the address of a pointer to an int for correct type casting.

Manik Sidana
  • 2,005
  • 2
  • 18
  • 29
  • +1 for pointing out all the problem I have. So the void pointer still can't de-reference, even after the type casting. Am I right? – mko Oct 25 '12 at 09:41
1

you can typecast the pointer in place like this(in snippet 3)

printf("%d",*(int*)ptr);

In all 3 snippets you're dereferencing void pointers. which isn't allowed by standards(or is undefined behavior, I don't remember).

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
1

Snippet 1 doesn't make any sense. You are trying to store something into "the void", which is impossible.

Snippet 2 makes sense, but since you can't take the contents of a "pointer to the void", *ptr cannot be used. Instead, cast it to a pointer to int, then take the contents of that. printf("%d", *(int*)ptr);

Snippet 3 doesn't make any sense, the type cast is not valid C syntax. Apart from that, it has the same problem as snippet 2.

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

dereferencing void pointer is undefined behaviour

You should try this

ptr=(int*)&a;

create one pointer of type int

and then assign this ptr to that and you can dereference that pointer

int *p;
p=ptr;
printf("%d",*p);
Omkant
  • 9,018
  • 8
  • 39
  • 59
  • thanks for your quick reply, is anyway can put the type casting operator on the left side of the void pointer, something like snippet 3 shows `*(int *)ptr = 1;` – mko Oct 25 '12 at 09:28
  • you have to use as i mentioned ..but still you can't use in printf as *ptr... because it's also dereferncing a void pointer... – Omkant Oct 25 '12 at 09:33
  • @yozloy- And what is the purpose for that? – Abhineet Oct 25 '12 at 09:33
  • You have to convert it anyhow for your use in this case – Omkant Oct 25 '12 at 09:36
  • @Omkant again thanks for answering me all the questions these days. So the void pointer still can't de-reference, even after the type casting. Am I right? – mko Oct 25 '12 at 09:39
  • 1
    `ptr=(int*)&a;` This cast does nothing, since a void pointer can be converted to any other pointer type without a cast. Which you do further down, when you write p=ptr. – Lundin Oct 25 '12 at 09:41
  • @lundin : yeah you are right, but it's safe to show like this so that coder himself is aware of what kind of address he is assigning to the void pointer .. anyway ptr=&a or ptr=(int*)&a is same. Readability matters sometimes in code – Omkant Oct 25 '12 at 09:44
  • @Lundin thank for pointing out this point, it's indeed useless to do that – mko Oct 25 '12 at 09:44
  • @yozloy: Yeah but see my comments – Omkant Oct 25 '12 at 09:44