addressing multiply arrays in C

Discussion in 'Mac Programming' started by farmerdoug, Jan 16, 2010.

  1. farmerdoug macrumors 6502a

    Sep 16, 2008
    Say I have several arrays A1, A2, A3. I want to make strings A1, A2, A3, that can be used to address elements in the arrays. Can I do it in a single routine?
  2. spatry macrumors newbie


    Sep 4, 2008
    You'll have to be more precise... I'm not sure what you mean. What I understand is that you have 3 arrays (1 dimension each) called A1, A2 and A3 and I assume they are of type char, hence a string. Dereferencing ([]) will yield a char and not a string.

  3. farmerdoug thread starter macrumors 6502a

    Sep 16, 2008

    Can you use some thing like strcpy and strcat to create a string that could be used in for loop

    for i ...
    for j ...
    A1[j] =

    I'm not sure I actually want or need to do this but I was wondering.
  4. LtRammstein macrumors 6502a


    Jun 20, 2006
    Denver, CO
    In C you can either make an array of characters (char) or you can use the string class (string.h). Either way a string is an array of char, so using proper for-loops will work.
  5. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    If you have three character arrays, and you want to access them all through a single array/name, you can declare an array of char* and assign the base of the arrays to the elements of the new array. On my phone, but I'll try to get some code written:
    char A1[8],A2[8],A3[8];
    char *As[3];
    As[0] = A1;
    As[1] = A2;
    As[2] = A3;
    A1 can now be accessed via As[0], etc.

  6. farmerdoug thread starter macrumors 6502a

    Sep 16, 2008


    I thought about that but that circumvents the original question. You took me too literally. What if there are 100 arrays and I don't want to assign 100 extra names?

  7. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    I may still be confused, but I think you're wanting to know if you can compose the actual names of variables you want to access at runtime. I.e. When x == 7 access A7. If this is what you are asking, the answer is no. The only language I can think of (there are likely others I am unaware of) where you can do something like this is Java, using a technique referred to as reflection.

  8. chown33 macrumors 604

    Aug 9, 2009
    Even after reading the whole thread, I'm still not sure I understand what the OP is asking for.

    It's possible that Perl or Python could do it, since they can more freely convert between string and numeric types, and they have builtin string types and an eval function:

    It's possible that Objective-C could do it, maybe using Key Value Coding:

    It's also possible that all the OP wants is a way to use C strings assembled at runtime as keys in a table, to retrieve a particular array calloc'ed at runtime, that's then subscripted using numeric variables. In that case, CoreFoundation's strings, dictionaries, and arrays would probably work.

    Frankly, it's hard to know what's needed here, because we don't know anything about the problem that's being addressed, only about the OP's particular approach to solving it.

    To farmerdoug, please be more specific about the types (string or number) involved. Take your original statement:

    This is ambiguous at best.

    Every array and every variable has a type. If the array A1 has type "array of string", then anything stored into it or read from must be a string. Conversely, if an array A2 has type "array of float", then it can only contain floats, which are a particular numeric type. A2 can't store int or long types, except by first converting to float, and it can't store string types at all.

    So the first sentence, "Say I have several arrays A1, A2, A3." is missing an important qualifier: What types are the arrays?

    The second sentence begins, "I want to make strings A1, A2, A3, ". Aha, so A1, A2, and A3 are arrays of strings using a single subscript.

    The rest of the sentence, "... that can be used to address elements in the arrays." then defines the rest of the problem. But it doesn't give any examples. Exactly what would one of these strings be? Would the string "A1[n]" work? How about "A1[n+3]"?

    Since we have no examples, and you haven't described even a tiny fraction of the data, there's simply no context in which to understand the problem to solve, much less how to propose a solution to that problem.

    I'm still thinking that C is not the best language for doing this program. It's too low-level. You're having a lot of trouble just doing fundamental stuff like creating and freeing the arrays. That's usually a bad sign.

    Other languages like Perl or Python have builtin honest-to-$deity string types, not just C's array-of-chars-masquerading-as-strings. They also have builtin types for dictionaries and associative arrays (i.e. arrays subscripted by any type, not just numbers). And they don't need low-level memory management like calloc() and free(). Best of all, with CPU speed and memory as plentiful as they are, these languages are plenty fast.
  9. spatry macrumors newbie


    Sep 4, 2008
    C++ would be the natural progression. C++ 2003 has all the data structures you'll need.
  10. ScoobyMcDoo macrumors 65816

    Nov 26, 2007
    Austin, TX
    I think you are asking if you can create a table of strings. If so, yes - one way to do it:

    char StringTable[TableSize][StringSize];

    Then if you want say copy the string "Hi There" into the 3rd entry in the table you would write something like:

    strcpy( StringTable[2], "Hi There" );
  11. Sydde macrumors 68020


    Aug 17, 2009
    If you are creating a persistent object, my inclination would be to use a pointer array. I would do it something like this

    char **  makeStringArray( int  arraySize, int stringSize ) {
      char  **theArray;
      int     arrayCtr;
      // allocate some number of pointers
      theArray = malloc( sizeof( char* ) * arraySize );
      for ( arrayCtr = 0 ; arrayCtr < arraySize ; arrayCtr++ ) {
          *( theArray + arrayCtr ) = malloc( sizeOf( char ) * stringSize );
      return theArray;
    Then, when you have to access an element in a string in the pointer array, you could do it like

    aChar = *( *( anArray + anArrayIndex ) + aCharIndex );

    C will automatically scale indexes to the sizeof the pointer's targets.

    This has the advantage of allowing for a flexible data structure that can grow or shrink in size and can contain strings of variable size. The disadvantage is that you have to allocate and deallocate each char array separately.

Share This Page