is this posible by using lots of pointers? i want to be able to reference ints and chars from the same array.
thanks,
mike.
is this posible by using lots of pointers? i want to be able to reference ints and chars from the same array.
thanks,
mike.
Why? Ok, so since a character is smaller than an integer, just use an array of integers.
Or you could make a structure that has both types and make an array of those.
Or you could use an array of void pointers....
Anyway, if it's just single characters or single integers (per cell in the array) (ie: not strings) just use an array of integers.
Quzah.
Hope is the first step on the road to disappointment.
>Or you could make a structure that has both types and make an
>array of those.
Or to save some memory, use union.
Or create an array of ints and for chars use only the first 8 bits of the variables.
>Why?
i'm trying to write a program that allows you to input a mathmatical expression eg 5*8+9 that then converts it to reverse polish notation (aka postfix notation) so it can be evaluated on a stack. i know this has been done before but i want to try and figure out most of it by myself to increase the learning experience.
anyway the input string gets tokenised into some ints (the oprands) and some chars (the operators). i need to be able to reference both of these from the same array to convert to postfix form and then evaluate.
> Ok, so since a character is smaller than an integer, just use an >array of integers.
how?
can you cast a char pointer to an int pointer?
>Or you could use an array of void pointers....
how?
>Or to save some memory, use union.
how?
>Or create an array of ints and for chars use only the first 8 bits >of the variables.
how?
thanks
>i need to be able to reference both of these from the same
>array to convert to postfix form and then evaluate.
Just use an array of char and when you need to evaluate an integer, just say
val - '0';
To get the integer equivalent of the character representation. Or better yet, use either a stack or a binary tree to convert the expression and then evaluate. This way you will be able to evaluate integers greater than one digit and you don't have to worry about ints and chars since a node would pretty much look like this:
-PreludeCode:struct node { char token[SOME_ARBITRARY_VALUE]; struct node *left, *right; };
My best code is written with the delete key.
To do the thing you want, you could use Prelude's idea. For evaluating expressions, trees are used a lot, search for expression trees in your library or with a search engine.
>>Or to save some memory, use union.
>how?
The size of a union is the size of its biggest element, in this case it is the int. Variables var_char and var_int have the same start address in memory.Code:union array_element { char var_char; int var_int; };
>>Or create an array of ints and for chars use only the first 8 bits >>of the variables.
>how?
Code:/* assume an int is 4 bytes */ int array [N]; char byte; byte = (char) (array [i] & 0x000000FF);