Thread: making an array name a variable

  1. #1
    Zaarin
    Guest

    making an array name a variable

    Hi,

    I've asked this question in another topic, but people where getting confused with the other parts of the question, so I'll rewrite it by itself here.

    I want a user to be able to enter a name of an array, and then the program use that name.

    For example: The user enters the array name "office", and the function then works on an array called office[x][y].

    Or if the user entered "boats", the function would work with an array called boats[x][y].

    ==============================================
    An example of the code:

    the following line uses the array "office[x]". I want that to have been taken from the users input.
    if(stricmp(office[x], string) == 0)

    if the user had entered "boat" instead, the line would read:
    if(stricmp(boat[x], string) == 0)
    ==============================================
    I would love any help possible.

  2. #2
    Ethereal Raccoon Procyon's Avatar
    Join Date
    Aug 2001
    Posts
    189
    I don't think you can. I'm pretty sure the name of the array becomes totally meaningless after your program is compiled; the existence of variable names is basically just a programmer's convenience. At compile time all the names are turned into memory addresses, and cease to exist. (Although, I could be wrong.)

    Edit: You could do a very similar thing with an dynamic array of arrays by allowing the user to "create" his own arrays at runtime (and give them default names stored in variables, or have the user name them himself) - but they won't necessarily have to have the same names as in your code.
    Last edited by Procyon; 08-31-2001 at 09:22 PM.

  3. #3
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    There's no reason why you can't create an array of strings containing all your array names (you'd have to create this array yourself) to cross reference with your actual arrays.

    As I said in the previous thread you could use a struct to hold the arrays and the sting containing the arrayname for cross referencing.

  4. #4
    Zaarin
    Guest
    It turns out that I don't need the user to enter the name of a array to search, which makes it easier, but I'm still getting into a bit of trouble.

    The code generates the following error:
    (18) : error C2664: 'LookFor' : cannot convert parameter 1 from 'char [11][16]' to 'char []'
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

    I must've missed something, I've tried many variations already.

    The complete code is here:
    [code]
    #include <iostream.h>
    #include <string.h>

    //declare variables
    char string[16] = "";

    //function prototypes
    int LookFor(char [], char*);

    //functions
    void main()
    {
    char office[11][16] = {{"Pen"},{"Chalk"},{"Biro"},{"Crayon"},{"Ruler"},{ "Eraser"},{"Stapler"},{"Paper"},{"Whiteout"},{"Glu e"},{"Pencil"}};

    cout << endl << "Enter the string you wish to find: ";
    cin.getline(string, 16, '\n');

    LookFor(office, string); //call function
    }

    int LookFor(char office[11][16], char *string)
    {
    int x = 0;
    while (x < 11)
    {
    if(stricmp(office[x], string) == 0)
    {
    cout << "Found string: " << string << ", in" << " Row: " << x << endl;
    return 1;
    }
    else
    {
    x++;
    }
    }
    return -1;
    }

    [\code]

  5. #5
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    The problem is in how you are passing the array to the function. You'll have to let the function know that the array is two-dimensional -

    Code:
    #include <iostream> 
    #include <cstring>
    
    using  std::cout; 
    using std::cin;
    using std::endl;
    
    
    //declare variables 
    char string[16] = ""; 
    
    //function prototypes 
    int LookFor(char[] [16], char*); 
    
    //functions 
    int main() 
    { 
        char office[11][16] = {{"Pen"},{"Chalk"},{"Biro"},{"Crayon"},{"Ruler"},{"Eraser"},{"Stapler"},{"Paper"},{"Whiteout"},{"Glue"},{"Pencil"}}; 
    
        cout << endl << "Enter the string you wish to find: "; 
        cin.getline(string, 16, '\n'); 
    
        LookFor(office, string); //call function 
    
        return 0;
    } 
    
    int LookFor(char office[][16], char *string) 
    { 
        int x = 0; 
        while (x < 11) 
        { 
            if(stricmp(office[x], string) == 0) 
            { 
                cout << "Found string: " << string << ", in" << " Row: " << x << endl; 
                return 1; 
            } 
            else 
            { 
                x++; 
            } 
        } 
        return -1; 
    } 
    

    Also you may want to give your array (string) another name as if you want to use the newer c++ libraries giving your variable the name 'string' prevents the 'using namspace std;' shortcut as your name conflicts with the c++ string class.

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    10
    Thankyou Zen
    Go you big red fire engine!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Dynamic Array substitution in composite data variable
    By DavidDobson in forum C Programming
    Replies: 2
    Last Post: 08-12-2008, 04:29 AM
  3. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  4. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM