Thread: storing different variable types in arrays.........

  1. #1
    Unregistered
    Guest

    Question storing different variable types in arrays.........

    is this posible by using lots of pointers? i want to be able to reference ints and chars from the same array.

    thanks,
    mike.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >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.

  4. #4
    Unregistered
    Guest

    Question hmmmmmmmmmm

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

  5. #5
    Unregistered
    Guest

    Question 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

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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:
    Code:
    struct node
    {
      char token[SOME_ARBITRARY_VALUE];
      struct node *left,
                  *right;
    };
    -Prelude
    My best code is written with the delete key.

  7. #7
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    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?

    Code:
    union array_element
    {
        char var_char;
        int var_int;
    };
    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.

    >>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);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Variable types
    By C_ntua in forum C Programming
    Replies: 4
    Last Post: 06-18-2008, 04:13 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. Storing a name, what variable?
    By RoD in forum C++ Programming
    Replies: 2
    Last Post: 09-28-2002, 02:36 AM
  4. Variable size arrays
    By crag2804 in forum C Programming
    Replies: 4
    Last Post: 09-08-2002, 06:00 PM
  5. Replies: 8
    Last Post: 04-22-2002, 10:02 PM