We help IT Professionals succeed at work.

Pixel Plotting Problem

Medium Priority
Last Modified: 2008-02-01
I'm having problems with a book that shows you how to write games in C. To plot a pixel on the screen vga mode 13h must be entered which I can do. Then a pointer is created pointing to the video address:

      char * far ptr=(char * far) 0xA0000000L;

and a pixel is plotted with:


Well 'far' fails the compile and not using it causes the program to crash, so how do I solve this? The solution must be as fast and compact ideally. Thanks.
Watch Question

Unlock this solution and get a sample of our free trial.
(No credit card required)

I have a very similar construct in a program which works (using MSC version 6.00). Perhaps it would be relevant to know what compiler you are using, what environment (something on a PC (from the video address); DOS? Windows? 95?), and exactly what error the compiler emits for the pointer assignment attempt.


I tried kinkajous' solution and got the following error messages upon compiling:

syntax error before * (on line unsigned char * far etc)
video_ptr undeclared  (on next line, i.e. because of above error)

I'm using the djgpp200 compiler (a DOS port of GNU) in a DOS window in Windows 95.

I also had to change up the syntax of the code snip I used earlier. The compiler complains that we are trying to assign a long address to a char pointer. I changed the syntax and compiled successfully using: "unsigned char far * video_ptr = (unsigned char far *) 0xA0000000;". Notice that the type cast has been changed from "char far *" to "unsigned char far *" and the address being assigned is devoid of the "L" indicating that it is a long address. Please let us know if this solves your problems.

Have you considered using assembler?  would be easier to implement, and you intergrate it into your C program.


I tried kinkajou's second solution but still got the error - I think the problem is with a 'far' pointer are they part of ANSI C?
If assembler is as quick then I'll use that, I take it I can write this inline? What do I do then?

"far" is not ANSI C. The intent of "far" is to provide a 32 bit pointer that can be placed in remote segments without declaring a memory model that utililzes 32 bit far pointers.

An Assembler routine would look like:

video_address EQU 0A000h

PUBLIC Plot_Pixel
Plot_Pixel PROC FAR C color:WORD

mov AX, video_address       ; Set ES:DI to screen memory
mov ES, AX
xor  DI,DI                           ; Zero DI

mov CX, 1                          ; Number of WORDS to display
mov AL, BYTE PTR color    ; Move the color into AH and al
mov AH,AL
rep  stowsw                       ; Write the color CX number of times using STRING
                                        ; WORD call

Plot_Pixel ENDP


But before doing any Assembler, I would suggest looking into your compiler options to change the memory model to MEDIUM or greater and type cast the video_ptr to an "unsigned long int". You might want to look up "far pointers" to see how your compiler suggests using them.
FAR pointers are only needed when you are making a 16 bit DOS program (or something else 16 bit).
the syntax:

 char far *video=(char far *)(0xa0000000);

works ok for borland under dos, don't know about others.
for watcom, this would also work (provided compiling 16 bit), but then you wouldn't need 16:16 pointers anyway.

if your memory model is large or something similair, you don't have to use the far keyword, since all pointers are far by default.

now for relevant stuff :)

just a hunch, but DJGPP is a 32 bit compiler. thus, the video address would be 0xa0000 and the FAR is not needed, and not wanted either. FAR as said earlier isn't a ANSI c thing, and it's quite possible djgpp doesn't know it. if so, the construct becomes:

char *video=(char *)0xa0000;



PS: although i like assembler and use it frequently, i do not think it is needed here :)


Nothing seems to work. How difficult can it be to plot a single pixel on the screen :)

So what is your status? Still getting errors? Correct me if I'm wrong, but aren't you using the DOS video memory address? That would indicated that you need to use a DOS executable and run it in MSDos. I'm not sure of the reliability of using the DOS video memory address under windows, even in a DOS box, when windows uses GDI to display to the screen. Let us know what is working and not for you. It may be a djgpp200 compiler issue.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.