here is a code

#include<stdio.h>
int main(void)
{
int b[10][5] ;
int (*q)[10][5] = &b ;
printf("%d", (char*)(q+1) -(char*)q);
}

the output it produces is 200 while if I change the typecast to (int *)(q+1)-(int *)q, the output is 50. I know that q is a pointer to the 2D array b. So q+1 is address immediately after the 50 elements but how does the type casting affects the output. Please explain in detail.

So q+1 is address immediately after the 50 elements but how does the type casting affects the output.

Pointer subtraction takes the size of the type into account. If it's a pointer to char, the size is 1 and with 50 chars the result will be 50. If it's a pointer to int, the size is sizeof(int), and with 50 ints the result will be 50 * sizeof(int).

On your system sizeof(int) looks to be 4, and 50 * 4 = 200.

Pointer subtraction takes the size of the type into account. If it's a pointer to char, the size is 1 and with 50 chars the result will be 50. If it's a pointer to int, the size is sizeof(int), and with 50 ints the result will be 50 * sizeof(int).

On your system sizeof(int) looks to be 4, and 50 * 4 = 200.

Yeah the sizeof(int) is 4 on my system. But what you are saying is exactly the opposite of what the output is. The output for (char *) is 200 and for (int *) is 50.

#include <stdio.h>

int main(void)
{
   int b[10][5] ;
   int (*q)[10][5] = &b ;
   printf("%p - %p = %d chars\n", q+1, q, (char*)(q+1) - (char*)q);
   printf("%p - %p = %d ints\n",  q+1, q, (int*) (q+1) -  (int*)q);
   return 0;
}

/* my output
0022FF68 - 0022FEA0 = 200 chars
0022FF68 - 0022FEA0 = 50 ints
*/
commented: LoL, exactly the same memory addresses on my computer :P +12

But what you are saying is exactly the opposite of what the output is. The output for (char *) is 200 and for (int *) is 50.

You are right. Sorry, I was explaining how the sizes added up instead of explaining your output.

Subtracting pointers will always count the number of objects between address A and address B, not the number of bytes. The size of an object is dependent on the type of the pointers being subtracted. If you use char pointers to get the difference, each object will be a byte. If you use int pointers, each object will be 4 bytes:

#include <stdio.h>

int main()
{
    int a[10];

    printf("%d\n", (int*)(a + 10) - (int*)a);
    printf("%d\n", (char*)(a + 10) - (char*)a);

    return 0;
}

The type of the array is int, so the size of the array is 10 * 4. If you subtract the first address and the last address using an int pointer, the result will be 10 because the size of each object is 4. If you subtract the same addresses with char pointers, the view changes so that each object is only 1 byte, and the result will be 40 because 10 * 4 bytes is 40 bytes.

You can do the same thing in reverse:

#include <stdio.h>

int main()
{
    char a[40];

    printf("%d\n", (int*)(a + 40) - (int*)a);
    printf("%d\n", (char*)(a + 40) - (char*)a);

    return 0;
}

Instead of an int array of 10, it's a char array of 40. The size of the array in bytes is the same, and the output is the same: 10 objects using int pointers, 40 objects using char pointers.

>The type of the array is int, so the size of the array is 10 * 4.

To the OP:
Yes, on most implementations, this is the case, but certainly not on all.
In that example you assume that an integer is 4 bytes.
You can get the exact size, by multiplying the number of elements in the array by sizeof(int) (or you can just put the following instruction in your program: [B]sizeof(array)[/B] , but this will only work with arrays which aren't on the heap of course)

(Tom, I'm sure you know that, but maybe the OP doesn't, though on his implementation it seems to be that integers are 4 bytes)

In that example you assume that an integer is 4 bytes.

No assumptions, we already established that sizeof(int) is 4 on his machine. I went ahead and used 4 instead of sizeof(int) for the maths because it would just add unnecessary complexity where hard numbers simplify everything.

I do not think it's relevant to this thread, but it is good to re-emphasize that type sizes are not fixed whenever you can. It confuses a lot of people who only work with PCs. ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.