Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Portability of a C code

Tags:

c

portability

I have the following code

int main()
{
   int a=6;
   void *p;
   p=&a;
   p++;
}

Does the void pointer here increment by a particular value (if it is holding the address of any data type) ?

In the above case p increments by 1 even though it is pointing to an integer value. According to me the above code invokes Implementation Defined behavior.

like image 559
Ashish Yadav Avatar asked Dec 12 '22 23:12

Ashish Yadav


1 Answers

The code in not valid from standard C point of view. It is illegal to increment void * pointers, or any other pointers to incomplete types.

Your compiler implements it as an extension, which is, of course, non-portable.

like image 56
AnT Avatar answered Jan 01 '23 01:01

AnT