edited by
665 views
0 votes
0 votes

6. Would the following program compile?

main( )
{
int a = 10, *j;
void *k;
j = k = &a;
j++ ;
k++;
printf ("\n %u %u", j, k ) ;
}

Please explain above program with some examples..

edited by

1 Answer

1 votes
1 votes

The reason we define type of pointer is that during arithmetic of pointers it should know by how much size it should increment or decrement. Now j and k storing address of a (eg address of a is 100). but when we do j++ and k++ j will be j=j+4(sizeof(int)=4 just example) whereas k will be incremented by one. So output will be j=104 and k=101.

Arithmetic with void pointer doesn't make sense.

https://stackoverflow.com/questions/16336757/why-does-incrementing-a-void-pointer-by-1-moves-one-byte-ahead-but-its-4-bytes

edited by

Related questions

1 votes
1 votes
1 answer
1
ermp888 asked Jun 30, 2018
836 views
(5) Would the following program give a compilation error or warning?main() { float i = 10, *j; void *k; k=&i; j= k; printf("\n%f", *j); }
1 votes
1 votes
2 answers
2
ermp888 asked Jul 1, 2018
513 views
13. What does the error "Null Pointer Assignment" mean and what causes this error?
0 votes
0 votes
2 answers
3
ermp888 asked Jun 27, 2018
1,654 views
Can you write another expression which does the same job as ++*ptr ?
1 votes
1 votes
1 answer
4