| How can you store a number such as 0x4c00 in a pointer?

Previous | Memory, C Variables, and Pointers | Next

Q: | According to what I have read, you cannot store an immediate value (or any non-pointer variable) to a pointer variable. You can only copy pointers of the same type and such. Now, the books may say this, but, is there a way to do this somehow? I want something like `'address=0x001fca;'` where address is a pointer to a char. Is it necessary to use the assembler for this?
A: | No. C is enough low-level language that nearly everything which can be done in ASM can be done in pure C too (of course, pure ASM code is usually faster). In your concrete example, using of typecasting is quite enough:
    address = (char*)0x001fca;
This is really a classic usage of the typecast operator. It is extremely powerful in C language, so it may be used to convert nearly everything to anything. This sometimes may be really terrible. For example, in TIGCCLIB implementation, I used very strange typecasting to convert an array to a function. And, nearly all TIGCCLIB functions are implemented using a typecast operator which constructs an indirect function call. For example, when you use something innocent like
    DrawStr (0, 0, "Hello", A_NORMAL);
the preprocessor expands it into a really terrible typecast. See _rom_call for more info.