A89: Re: Newbie questions (TI-GCC)


[Prev][Next][Index][Thread]

A89: Re: Newbie questions (TI-GCC)




Hi!

> Why doesn't this work?
>
> void * screen = LCD_MEM;
> *pointer = 0xFF;

You probably mean *screen = 0xFF; ?

> When I do this, I get the error "Invalid use of void expression" 
> at the second line. Can't you assign a value to a dereferenced void
> pointer?

As you can see, no, without an explicite typecasting, because the
compiler can't know the size of an object pointed to by void pointer.
You can do an explicite typecast:

*(char*)screen = 0xFF;

Or better, if you need to dereference it often, then declare

unsigned char * screen = LCD_MEM;

i.e. avoid void pointers. See the next point to see why such assignment
is legal (assigning LCD_MEM which is void pointer to a char pointer).

> If not, what good is a void pointer?

1) void pointers are used mainly as arguments of functions which
represents memory addresses, no matter what is the object located at
this addresses. Then, any pointer type (including arrays, which are
in fact pointers to the first element of the array) may be passed
to such function without warnings and without needness for explicite
typecasting. For example, memcpy is such function, and it is declared
as:

void * memcpy (void * source, void * destination, unsigned long len);

Ignore returned type for a moment. So, you can do

memcpy (LCD_MEM, buffer, 3840);

but you also can do

memcpy (a, b, 10 * sizeof(long));

assuming that you have

long a[10], b[10];

2) void pointers may be assigned to any other pointer type and vice
versa without and warnings and without needness for explicite
typecasting. They are usually returned as the result of functions
which don't make any assumptions what will be purpose of returned
pointer. For example, malloc is such function. It is declared as

void * malloc (unsigned int len);

So, assuming that you have

char *a;
int *b;
long *c;

you can do:

a = malloc (100);
b = malloc (30 * sizeof(int));
c = malloc (50 * sizeof(long));

> Is it possible to add two bytes to a longword pointer?  I am using
> a longword pointer to write longwords to the screen, and I need to
> add 30 bytes to it to get to the next line. However, I can only add
> multiples of four bytes to it! Help!

It is possible using typecasting:

ptr = (long*)((char*)ptr + 30);

Don't be afraid, the compiler will generate just addition: everything
other is just to satisfie type checking conventions.

Or, I think that this also may work:

(char*)ptr += 30;

but I never checked this.

> Tired of newbie questions yet?

Absolutely not, except if the question is already answered in TIGCCLIB
FAQ list...

> I hope not, because I have more where those came from :-)  We 
> _really_ need a website for TI-89 C programmers!

Yes.

> It could have a tutorial and an FAQ page so that questions like these
> could be answered without bugging all you patient programmers on this 
> list.

But who will wrote a tutorial? I have not enough time :-(

Cheers,

Zeljko Juric



Follow-Ups: