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

Explain which function in c can be used to append a string to another string?

590


Why is c still so popular?

618


What is declaration and definition in c?

525


What is function what are the types of function?

560


Explain what could possibly be the problem if a valid function name such as tolower() is being reported by the c compiler as undefined?

585






If errno contains a nonzero number, is there an error?

805


What is multidimensional arrays

632


What are global variables and how do you declare them?

621


I have seen function declarations that look like this

602


in case any function return float value we must declare a) the function must be declared as 'float' in main() as well b) the function automatically returned float values c) function before declared 'float' keyword d) all the above

600


What is size of union in c?

581


List some basic data types in c?

561


write a program to input 10 strings and compare without using strcmp() function. If the character of one string matches with the characters of another string , sort them and make it a single string ??? example:- str1="Aakash" st2="Himanshu" str="Uday" output:- Aakashimanshuday (please post the answer as quickly as possible)

1627


How can I remove the trailing spaces from a string?

616


What is memory leak in c?

637