Agreed.Solar wrote: Because that construct is correct.
The thing is, when writing portable C, you should never need to come up with a pointer to a specific address.But try to come up with a char* to 0xb8000 that doesn't throw a warning with -pedantic. The solution isand potentially non-portable because pointer arithmetics are only defined within the same array (and there is no char* array starting at 0 because 0 must not point to a valid object). Add to this that ptr is not required by the standard to actually point to address 0 (as we discussed in this thread)... you could, theoretically, end up just about anywhere.Code: Select all
char* ptr = 0; ptr += 0xb8000;
Anyhow, are you sure pointer arithmetics are not defined outside arrays.. I've never seen anything that would claim this to be true, and the point is, it makes malloc useless for things like strings... then again.. there's calloc().
Let's all from now on allocate our strings with calloc(len,sizeof(char)); We should also probably not rely on char being 8bits or int 32bits since char could just as well be 7bits and int 25bits, or pretty much anything else, as long as sizeof(char)<=sizeof(short)<=sizeof(int)<=sizeof(long).
This actually gave me an insight. At some point I'm supposed to have a uni class of C. I happen to know that the professor happens to want (supposedly) standard C99 so I better remember all the pain of using pointers portably then. He's gonna have fun with that calloc()..