in Programming in C
4,031 views
1 vote
1 vote
consider the two declarations

void *voidPtr;

char *charPtr;

which of the following assignments are syntactically correct?

(a)charPtr= voidPtr

(b)*charPtr=voidPtr

(c)*voidPtr=*charPtr

(d)voidPtr=charPtr

 

please explain!
in Programming in C
4.0k views

25 Comments

as per my knowledge, option D is correct.

what is the given answer?
0
0
why option a is not correct?
0
0

i didn't found any problem with option a also

0
0

@Shaik Masthan

Shouldn't   A and B should also be correct ?

0
0

@Shiv Gaur

OPTION B should be wrong

0
0
Option B and C are not correct :

because

charPtr is indicates a pointer points to a char.

voidPtr is a pointer points to void ,

since *charPtr is character it will not store an adress of void,

similarlly option C is incorrect
1
1

@balaganesh

what is the answer they provided?

0
0
option D is the answer.
0
0

@balaganesh

i hope they mistakenly type option a

(a) charPtr= voidPtr

instead of 

(a) *charPtr= *voidPtr

if this is the original option, then only Option D is correct,

otherwise option A and Option D , both are correct 

0
0
then what about c)?
0
0

srestha de-referencing void pointer is not allowed so *voidPtr will give error

0
0

you can't directly de-reference the void pointer

correct syntax of option C ===> *(char *)voidPtr=*charPtr

1
1
@MiniPanda any standard resource about it?
0
0

@Shaik

*charPtr= *voidPtr

then how do we dereference a char pointer and assign a void pointer value?

 

0
0

Some interesting facts point 1

https://www.geeksforgeeks.org/void-pointer-c/

 

1
1

*charPtr= *voidPtr

this is incorrect 

0
0

int main()

{

    int a = 10;

    void *ptr = &a;

    printf("%d", *ptr);

    return 0;

}

 

See this, it gives compiler error

then how d will be correct 

0
0
We are not de-ferencing void pointer in D.. we are just copying the address of a char variable into void pointer..that is allowed
1
1
ok , thank u :)

can u tell me one last thing, where this copying is actually.

I think, I havenot seen this before
0
0

See this..

1
1

can u tell me one last thing, where this copying is actually.

i didn't get it

0
0
@Minipanda

is it like call by reference?
0
0

@Shaik

I mean one memory adress is copying in another pointer variable

where practically we need to do this?

0
0

is it like call by reference?

Yes kind of.. but like in call by ref, the pointer can manipulate the value by de-referencing but in this case we cannot change value of c by *voidPtr.

1
1

where practically we need to do this?

we can use, void pointer for implementing function overloading in C

1
1

1 Answer

0 votes
0 votes
voidptr = charptr

Related questions