M
matt
I (think I have) understood the pitfalls of pointer arithmetics.
For example, the output of this code depends on the platform on which
this program will run:
char * cp = "Hello World";
char * c2p = NULL;
int * ip = (int *) cp;
ip = ip + 1;
c2p = (char *) ip;
printf("%c\n", *c2p);
The output of this program is "o" where the size of integer is 4 and "l"
where the size of integer is 2 bytes. However, this problem is intrinsic
in the application, where we scan an array of chars with a pointer to
integers (through the typecasting).
On the other hand, is the following (pseudo)code
mytype mta[5];
...
mytype * mtp = &mta; // 0 <= i <= 4
mytypeAmazingFunction(mtp, mtp-1, <some_other_args>);
*always* portable to any architecture?
It is equivalent to
mytypeAmazingFunction(&mtp, &mtp[i-1], <some_other_args>);
which is always portable, isn't it?
For example, the output of this code depends on the platform on which
this program will run:
char * cp = "Hello World";
char * c2p = NULL;
int * ip = (int *) cp;
ip = ip + 1;
c2p = (char *) ip;
printf("%c\n", *c2p);
The output of this program is "o" where the size of integer is 4 and "l"
where the size of integer is 2 bytes. However, this problem is intrinsic
in the application, where we scan an array of chars with a pointer to
integers (through the typecasting).
On the other hand, is the following (pseudo)code
mytype mta[5];
...
mytype * mtp = &mta; // 0 <= i <= 4
mytypeAmazingFunction(mtp, mtp-1, <some_other_args>);
*always* portable to any architecture?
It is equivalent to
mytypeAmazingFunction(&mtp, &mtp[i-1], <some_other_args>);
which is always portable, isn't it?