Page 1 of 3
char *vidmem=(char *)0xb8000
Posted: Fri Jul 04, 2003 10:06 am
by slacker
why do you need the "(char *)" in front of the address cause shouldnt you be able to leave out this part?
Re:char *vidmem=(char *)0xb8000
Posted: Fri Jul 04, 2003 10:10 am
by Therx
You can leave it out but GCC (with -Wall) will warn that you are making a pointer from integor. For a clean build you don't want this clogging the display
Re:char *vidmem=(char *)0xb8000
Posted: Fri Jul 04, 2003 10:14 am
by slacker
why would you be making a pointer from an integer?
Re:char *vidmem=(char *)0xb8000
Posted: Fri Jul 04, 2003 11:23 am
by Nairou
Because 0xb8000 is a number, an integer, while *vidmem is a pointer. Integers and pointers are not the same thing, so the compiler will give you a warning about it. In this case it would still work, but its best to eliminate all warnings, both for cleanliness and stability (you never know when a warning will be the cause of a bug you're trying to find). By putting the (char*) in front of the integer, you are telling the compiler that you really do know what you're doing, and you really do want to put the integer into the pointer.
Re:char *vidmem=(char *)0xb8000
Posted: Fri Jul 04, 2003 11:40 am
by slacker
but by writing char *vidmem your saying that vidmem will be a pointer to a char. you already told the compiler that your pointing at a char...why do you need to tell it again?
Re:char *vidmem=(char *)0xb8000
Posted: Fri Jul 04, 2003 11:50 am
by Therx
If you consider setting it to a lower int
char *ptr;
ptr = 100;
The compiler is warning that you may have meant.
char *ptr;
*ptr = 100;
these have completely different results. The (char *) simply tells the compiler that you definately want the first. I know that once my whole device manager would not work for the sake of one * amongst over 250 lines of code. Unfortunately it was passing an argument to a function so I didn't get a warning and I eventually had to post it here to find the bug.
Re:char *vidmem=(char *)0xb8000
Posted: Fri Jul 04, 2003 12:10 pm
by slacker
k now im really confused.
char *pntr;
*pntr=0xb8000; <----means that the pointer will be referencing memory at that specific location.
now if you say:
pntr='a';
that means that the value at that location is 'a';
i thought this is how it works?
Re:char *vidmem=(char *)0xb8000
Posted: Fri Jul 04, 2003 12:23 pm
by Therx
No
ptr = 0x80000
Sets to pointer to 0x80000
Then
*ptr = 'a'
would set the byte at 0x80000 to 'a'
Re:char *vidmem=(char *)0xb8000
Posted: Fri Jul 04, 2003 12:26 pm
by slacker
im pretty sure its the other way around...
Re:char *vidmem=(char *)0xb8000
Posted: Fri Jul 04, 2003 12:27 pm
by slacker
yea i found a site..your right
Re:char *vidmem=(char *)0xb8000
Posted: Fri Jul 04, 2003 3:12 pm
by slacker
my compiler wont let me do this:
char *vidmem;
vidmem=0xb8000;
why?
Re:char *vidmem=(char *)0xb8000
Posted: Fri Jul 04, 2003 4:27 pm
by Tim
Because you're trying to make a pointer from an integer
.
vidmem is of type char*, and 0xb8000 is of type int. You can't assign an int to a char* -- they're just different types.
Now, it happens that, on the x86 CPU in 32-bit pmode, ints and char*s are the same size. So what you need to do is tell the compiler, "I know what I'm doing, leave me alone", and put in the (char*) cast.
Re:char *vidmem=(char *)0xb8000
Posted: Fri Jul 04, 2003 4:59 pm
by df
Tim Robinson wrote:So what you need to do is tell the compiler, "I know what I'm doing, leave me alone"
heheh i like that. I'm not casting! I'm bossing the compiler around!
Re:char *vidmem=(char *)0xb8000
Posted: Fri Jul 04, 2003 5:16 pm
by slacker
but the pointer is the size of an int. the char says the pointer is pointing to a char at a certain address. doesnt the pointer know that the only type of memory address it can accept to point to is an int?
Re:char *vidmem=(char *)0xb8000
Posted: Sat Jul 05, 2003 6:13 am
by Tim
No. The only values you can assign a pointer are: another pointer of the same type, a void pointer (but not in C++), or the value 0 (NULL). Assigning an int to a pointer is a hack which will only work on machines with a flat address space, so you need the cast to let you do it.