Machine-translated by Google. Questions are authored in English.
Top

Discussion

Will the program compile in Turbo C?

#include <stdio.h>
int main()
{
    int a=10, *j;
    void *k;
    j=k=&a;
    j++;
    k++;
    printf("%u %u\n", j, k);
    return 0;
}

 

  • A.Yes
  • B.No

Answer: B

Correct answer: No

Arithmetic on a void * is not allowed, because the compiler would have to know the size of the pointed-to type to advance the pointer, and void has none. So k++ is the offending line. The j++ above it is perfectly legal and advances by the size of an int. GCC permits void arithmetic as an extension, treating the size as one, but it is not standard C.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.