c - Why does incrementing a void pointer by 1 moves one byte ahead but it's 4 bytes for an integer pointer,8 for double? -
this question has answer here:
- pointer arithmetic void pointer in c 7 answers
in following program,if add 1 void pointer, moves 1 byte ahead.but,quite expected, moves 4 , 8 bytes respectively int
, double
pointers.why void pointer move 1 byte,just character pointer would?
#include<stdio.h> int main(void) { int num=3,*int_ptr=# double sum=3,*double_ptr=∑ void *void_ptr1=&num,*void_ptr2=∑ printf("%p,%p,%p,%p,%p,%p,%p,%p",void_ptr1,void_ptr1+1,\ void_ptr2,void_ptr2+1,int_ptr,int_ptr+1,double_ptr,double_ptr+1); }
you can't pointer arithmetic on void pointer (because doesn't make sense). it's compiler has extension allows pointer arithmetic performed on void pointers, , it's implemented this. however, neither standard nor encouraged used.
Comments
Post a Comment