char *vidmem=(char *)0xb8000

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
slacker

char *vidmem=(char *)0xb8000

Post by slacker »

why do you need the "(char *)" in front of the address cause shouldnt you be able to leave out this part?
Therx

Re:char *vidmem=(char *)0xb8000

Post 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
slacker

Re:char *vidmem=(char *)0xb8000

Post by slacker »

why would you be making a pointer from an integer?
Nairou

Re:char *vidmem=(char *)0xb8000

Post 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.
slacker

Re:char *vidmem=(char *)0xb8000

Post 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?
Therx

Re:char *vidmem=(char *)0xb8000

Post 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.
slacker

Re:char *vidmem=(char *)0xb8000

Post 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?
Therx

Re:char *vidmem=(char *)0xb8000

Post by Therx »

No

ptr = 0x80000
Sets to pointer to 0x80000

Then
*ptr = 'a'
would set the byte at 0x80000 to 'a'
slacker

Re:char *vidmem=(char *)0xb8000

Post by slacker »

im pretty sure its the other way around...
slacker

Re:char *vidmem=(char *)0xb8000

Post by slacker »

yea i found a site..your right
slacker

Re:char *vidmem=(char *)0xb8000

Post by slacker »

my compiler wont let me do this:

char *vidmem;
vidmem=0xb8000;

why?
Tim

Re:char *vidmem=(char *)0xb8000

Post 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.
User avatar
df
Member
Member
Posts: 1076
Joined: Fri Oct 22, 2004 11:00 pm
Contact:

Re:char *vidmem=(char *)0xb8000

Post 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!
-- Stu --
slacker

Re:char *vidmem=(char *)0xb8000

Post 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?
Tim

Re:char *vidmem=(char *)0xb8000

Post 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.
Post Reply