J
junky_fellow
Can I subtract two pointers of same type that are pointing to the two
different location of memory allocated by malloc.
eg.
#include <stdlib.h>
int main(void)
{
unsigned char *c_ptr;
unsigned char *c_ptr1;
unsigned char *c_ptr2;
int diff;
c_ptr = malloc(100 * sizeof(char));
c_ptr1 = c_ptr + 5;
c_ptr2 = c_ptr + 10;
diff = c_ptr2 - c_ptr1; /* Is it valid ? */
}
Is the subtraction ( c_ptr2 - c_ptr1 ) valid ?
Is it guaranteed that "diff" will always be 5 ?
different location of memory allocated by malloc.
eg.
#include <stdlib.h>
int main(void)
{
unsigned char *c_ptr;
unsigned char *c_ptr1;
unsigned char *c_ptr2;
int diff;
c_ptr = malloc(100 * sizeof(char));
c_ptr1 = c_ptr + 5;
c_ptr2 = c_ptr + 10;
diff = c_ptr2 - c_ptr1; /* Is it valid ? */
}
Is the subtraction ( c_ptr2 - c_ptr1 ) valid ?
Is it guaranteed that "diff" will always be 5 ?