Thread: I dont even know how to ask this question :(. Array and strings?

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    12

    I dont even know how to ask this question :(. Array and strings?

    Ok I have an array but I need to get a char array and make it actually equal the name of the array and use it accordingly. For example.

    int hello[3];

    char nameofarray[]="hello";

    I want it to make it so that I can call nameofarray by using something like:

    nameofarry[]

    Is there anyway to do something like this? I know its kinda far fetched but it would really help me out.

  2. #2
    Registered User
    Join Date
    Jul 2004
    Posts
    91

    Cool

    im not sure about everyoen else, but i read your post a few times, and im not sure what your trying to say..?
    what do you want to do with nameofarray ?

  3. #3
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    same here.

    This line confuses me :
    Code:
    int hello[3];
    what are you trying to accomplish here? are you looking for a way to type the contents "nameofarray"? or maybe let the length of "nameofarray" array equal "hello" array?

    please re-phrase your question if possible
    Last edited by Brain Cell; 07-30-2004 at 04:32 AM.
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    101
    It appears as if you wish to select an array to use dynamically through a single name, nameofarray. In other words, a map with the name of the array as the key and the address of the array as the value. This is possible, though not nearly as easy as you might like. Here is one way to go about it.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    struct map {
      char name[BUFSIZ];
      int (*array)[3];
    };
    
    int map_index(const char *name);
    
    int main()
    {
      int a[3] = {0};
      int b[3] = {0};
      struct map nameofarray[] = {
        "a", &a,
        "b", &b
      };
      int which = map_index("a");
    
      if (which != -1) {
        *(nameofarray[which].array)[0] = 10;
        printf("a[0]: %d\n", a[0]);
        printf("b[0]: %d\n", b[0]);
      }
    
      return 0;
    }
    
    int map_index(const char *name)
    {
      if (strcmp(name, "a") == 0) {
        return 0;
      }
      else if (strcmp(name, "b") == 0) {
        return 1;
      }
      else {
        return -1;
      }
    }

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    struct array
    {
        char *name;
        size_t size;
        char *data;
    };
    Since I, like everyone else, have no idea what you're trying to do, I thought you might have some fun with this...

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    12
    Err ok let me try to explain it better:

    I have lets say 3 integers.

    int x1;
    int x2;
    int x3;

    Now I have a string. Lets call this string nameofint:

    char nameofint[2];

    I ask for an input from the user about what integer they want. The user then picks x1, x2, x3. Now nameofint is equal to x1, x2, or x3. So I wanna be able to make the int that the user picked equal another int that we will call uselessint.

    int uselessint=0;
    uselessint=nameofint;

    Since nameofint equals either x1, x2, or x3. I want it to take the value from the integer the user inputs and put it into uselessint.

    This isn't for dos this is actually for a very unknown device which i just felt like programming for while I was bored.

  7. #7
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    so something like thsi(without the x1,x2,x3)
    Code:
    int array[3];
        printf("Enter 3 numbers(integers)\n");
        scanf("%d %d %d",&array[0],&array[1],&array[2]);
    }
    with x1,x2,x3
    Code:
    int x1,x2,x3,array[3];
    printf("Enter 3 numbers(integers)\n");
    scanf("%d %d %d",&x1,&x2,&x3);
    array[0]=x1;
    array[1]=x2;
    array[2]=x3;

  8. #8
    Registered User
    Join Date
    Jul 2004
    Posts
    12
    Something more like this:

    int x1=5,x2=3,x3=4,
    char nameofint[3];
    printf("Enter name of integer\n");
    gets(nameofint);
    int uselessint;

    uselesss=nameofint;.//I want uselessint to equal 5 if user types in x1, 3 if he types x2,
    //and 4 if he types in x3

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    The easiest way to do something like that is like Princeton suggested.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      int x1 = 5, x2 = 3, x3 = 4;
      char nameofint[3];
      int i, uselessint;
      struct
      {
        char *name;
        int *pvar;
      } map[] = { { "x1", &x1 }, { "x2", &x2 }, { "x3", &x3 } };
    
      printf("Enter x1, x2, or x3: ");
      fgets(nameofint, sizeof(nameofint), stdin);
      nameofint[2] = '\0'; // Get rid of the \n
    
      for(i = 0;i < 3;++i)
        if(!strcmp(map[i].name, nameofint))
          break;
      if(i == 3)
      {
        puts("That variable name isn't recognized.");
        exit(1);
      }
    
      uselessint = *map[i].pvar;
      printf("uselessint is %d\n", uselessint);
    
      return 0;
    }
    itsme:~/C$ ./map
    Enter x1, x2, or x3: x1
    uselessint is 5
    itsme:~/C$ ./map
    Enter x1, x2, or x3: x2
    uselessint is 3
    itsme:~/C$ ./map
    Enter x1, x2, or x3: x3
    uselessint is 4
    itsme:~/C$ ./map
    Enter x1, x2, or x3: foo
    That variable name isn't recognized.
    itsme:~/C$
    Last edited by itsme86; 07-30-2004 at 02:45 PM.

  10. #10
    uninteresting
    Join Date
    Jun 2002
    Posts
    66
    You'd be using hash tables. Personally I would implement it with a struct:

    Code:
    struct hash {
       char name[10];
       int value;
    } hashtable[3];
    Then you can set the name and value of each element in the table. After that you can use a for loop to search for values.
    *** TITANIC has quit (Excess Flood)

  11. #11
    Adamant Programmer Axpen's Avatar
    Join Date
    Jun 2003
    Location
    USA
    Posts
    42
    I think I get what he's saying.

    You can do it like this:
    Code:
    int main(void)
    {
     int x1,x2,x3,uselessint;
     char nameofint[2];
    
     gets(nameofint);
    
     if(nameofint[0]=='x'&&nameofint[1]=='1'&&nameofint[2]=='\0')
     {
      uselessint=x1;
     }
     if(nameofint[0]=='x'&&nameofint[1]=='2'&&nameofint[2]=='\0')
     {
      uselessint=x2;
     }
     if(nameofint[0]=='x'&&nameofint[1]=='3'&&nameofint[2]=='\0')
     {
      uselessint=x3;
     }
    
     return 0;
    }
    (Notice I didn't use any string.h functions for compatibility for his "unknown device"'s compiler, yes I know that strcmp and most string.h functions are ANSI C, but not every compiler's ANSI )

    Sorry if you mean "on-the-fly variable names", but if you didn't, hope this helps .
    The Man With 3 Ears::Oh no better get the huskers

    Download Helppc by David Jurgens, It's a FANTASTIC Reference!!!

    In Case I Forget I Have:
    Windows XP
    For My 32-bit Questions:
    Dev C++ (mainly just use its mingw)
    For My 16-bit Questions:
    Borland Turbo C++ 1.01

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > gets(nameofint);
    Nope
    Read the FAQ / many other posts

    > Notice I didn't use any string.h functions for compatibility for his "unknown device"'s compiler
    Huh?
    Any compiler omitting string.h ceases to be a C compiler of any description IMO
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    Jul 2004
    Posts
    101
    >>Any compiler omitting string.h ceases to be a C compiler of any description IMO
    A freestanding implementation is not required to supply string.h. If this "unknown device" uses such an implementation then one cannot assume the existence of string.h.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    And what about the inexcusable gets() - will the free standing implementation provide that, and not string.h?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #15
    Registered User
    Join Date
    Jul 2004
    Posts
    101
    Quote Originally Posted by Salem
    And what about the inexcusable gets() - will the free standing implementation provide that, and not string.h?
    Possibly, it is up to the implementation at that point. However, my comment was not concerning Axpen's code but your statement that a compiler which omits string.h is not a C compiler "of any description", which is false because a freestanding implementation is not required to support string.h. Of course, you also said IMO so my reply was meant to give additional information to readers rather than to correct you. A statement of opinion can be inaccurate and still be entirely correct.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of strings question
    By FortinsM3 in forum C Programming
    Replies: 17
    Last Post: 02-15-2009, 04:02 PM
  2. array of strings?
    By mc72 in forum C Programming
    Replies: 5
    Last Post: 11-16-2008, 12:15 AM
  3. Replies: 2
    Last Post: 04-27-2008, 03:39 AM
  4. Array of strings in C
    By szill in forum C Programming
    Replies: 10
    Last Post: 02-22-2005, 05:03 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM