typecasting using pointer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • noob15
    New Member
    • Jun 2010
    • 13

    #1

    typecasting using pointer

    int *p, *q;
    p= (int *) 60;
    q= (int *) 20;
    printf("%d", p-q);


    the answer is 10

    m lil unsure about wat does (int *) 60 means

    if is it memory location numbered 60,as it seems...then it is possible that this memory area is not available so output should be undefined.

    need help with this conceptual doubt
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    The expression p-q involves pointer arithmetic. This pointer expression is only legal if p and q point to the same type, if p and q both point to actual variables of that type, and if p is greater than q. If those constraints are met, then the expression computes the number of variables of that type that fit in the memory region that starts at q and ends at p.

    The answer you got implies that sizeof(int) is 4.

    Comment

    Working...