Quick C question (arrays)

Discussion in 'Mac Programming' started by beefstu01, Jun 10, 2005.

  1. beefstu01 macrumors member

    Joined:
    Oct 24, 2002
    Location:
    CA
    #1
    Hey,

    I was hoping someone here could help me. Is there any way for you to find the size of an array in C like you can in Java? Dunno if I explained it clearly, so here's what I'm talking about:

    Java:
    Code:
    for(int i = 0; i < myArray.length; i++)
         (stuff)
    Is there any way for me to implement that in C without knowing the length of the array beforehand?
     
  2. IscariotJ macrumors 6502a

    Joined:
    Jan 13, 2004
    Location:
    UK
    #2
    You could try sizeof(). If you're trying to find your way round C, have a look at this - http://publications.gbdirect.co.uk/c_book/

    An easier way is probably to keep a count of the items in it, so that you know what size to realloc when you reach the limit.

    J
     
  3. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #3
    The problem is that straight C is not object-oriented. An array is simply a load of memory that you are happening to treat as an array. If you know what type of stuff is in the array the sizeof stuff can help. Another common practice is to terminate the array with a special character (Strings in C are arrays of characters with \0 termination).
     
  4. beefstu01 thread starter macrumors member

    Joined:
    Oct 24, 2002
    Location:
    CA
    #4
    I'll know exactly what I'll be operating on, so sizeof() will work for me. I'm making a function that takes in an integer array with random numbers in it, so I can't really have a termination character.

    Thanks a lot.
     
  5. savar macrumors 68000

    savar

    Joined:
    Jun 6, 2003
    Location:
    District of Columbia
    #5
    Contrary to what others have said, I don't think this is possible. An array in C is *not* an object, so how would it have any way to keep track of its own length?

    I tested the following program:
    Code:
    #include <stdio.h>
    
    main() {
     int testArray[] = {1, 2, 3, 4};
     printf("size test:%d\n",sizeof(testArray));
    }
    
    The answer I get is 16, which is 4 * 4 bytes per int. I'm guessing that the only reason I get this answer is because the array is statically initialized and so the compiler can treat is as a defined type.

    In C, there are two ways to iterate through an array. One is to keep track of the length of the array, and pass that value to any code which needs it. The other is to terminate the array with a sentinel value: i.e. if your array stores positive ints, make -1 signal the end of the array.

    This is why the signature for main() is

    int main(int argc, char **argv)

    Because if you want to loop through the arguments to your program, you need to know how many there are.
     
  6. jcgerm macrumors member

    Joined:
    May 28, 2003
    #6
    Why can't you just keep track of the size of the array? You have to know the size of the array to create it, so just keep track of it that way. You can either dynamically allocate it or statically allocate it, but either way you have to know the size to create it at some point.

    Jeremy
     
  7. broken_keyboard macrumors 65816

    broken_keyboard

    Joined:
    Apr 19, 2004
    Location:
    Secret Moon base
    #7
    Perhaps you could make the first slot in your array hold size - 1

    for(int i = 1; i < myArray[0]; i++)
    (stuff)
     

Share This Page