why arithmetic operation can’t be performed on a void pointer?

Answer Posted / vadivel t

Hav an example with an int pointer,
assume compiler allocates two bytes for an int.

int *iptr, iArray[3] = {1, 2, 3};
/*say the iArray starting address would be 1000*/
iptr = iArray[0];
/*When u r trying to do iptr++ then it will point to 1002
(two bytes for an int) where element 2 available.
cos compiler knows how many bytes has two increment*/
iptr++;

Now come to void:

void pointer is generic pointer which can be point to any
kind of data types.

void *ptr;

/*When u r trying to do ptr++, since it is void pointer, it
will not know exactly how many bytes has to be incremented.
So that arithmatic operations not possible with void
pointer.*/

Is This Answer Correct ?    4 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Where local variables are stored in c?

550


what are the advantages of a macro over a function?

633


Explain the use of keyword 'register' with respect to variables.

585


How to throw some light on the b tree?

600


Write a program to replace n bits from the position p of the bit representation of an inputted character x with the one's complement. Method invertBit takes 3 parameters x as input character, p as position and n as the number of positions from p. Replace n bits from pth position in 8 bit character x. Then return the characters by inverting the bits.

3683






write a program to reverse a every alternetive words in a string in a place. EX: Input is "this is the line of text" Output should be "shit is eht line fo text" Please any one tell me code for that.

1569


Explain what does the function toupper() do?

627


What is the use of printf() and scanf() functions?

622


Write a program for Overriding.

672


How can I recover the file name given an open stream?

547


What is the purpose of & in scanf?

586


Explain what are its uses in c programming?

587


what is ur strangth & weekness

1810


What is #line in c?

554


Is null always equal to 0(zero)?

574