1

I have an array of unsigned long long numbers. I initialize the first 12 elements. The array changes size throughout the programs runtime. How can I find out the length of the array.

I can't use sizeof(array)/sizeof(unsigned long long) since it doesn't work correctly in this case.

I tried using this loop:

count = 0;
while ( array[count] ) count++;
return count;

The problem with this method is that it still returns the wrong length.

Here is the basic rundown of the code.

unsigned long long *prime = (unsigned long long *)calloc(1000, sizeof(unsigned long long);
prime[0] = 3;
prime[1] = 5;
....
prime[11] = 41;
unsigned long long *ptrEmpty = &prime[12];
int sizeComp;

//User can change change the size of the array here to create a larger list of primes.//
for( i = 43, sizeComp = 12; sizeComp < length(prime); i += 2 ){
    // Do stuff...
} 

How can I find this length?

M.M
  • 138,810
  • 21
  • 208
  • 365
user1362058
  • 751
  • 1
  • 5
  • 14
  • 1
    Array lengths are always found out in one of two ways: either you specify it elsewhere (as a second parameter) and simply pass it in along with the array, or you come up with some sentinel value (traditionally NULL) that represents the end of the list. Those are your choices. – Chris Eberle Jun 07 '13 at 01:49
  • possible duplicate of [length of array in function argument](http://stackoverflow.com/questions/8269048/length-of-array-in-function-argument) – M.M Nov 03 '14 at 06:48

1 Answers1

5

One way is to keep a counter every time an element is inserted into the array. Maybe you can encapsulate the array in a struct e.g:

typedef struct int_arr
{
    int* arr_ptr;
    size_t max_size;
    int curr_elem;
} int_arr;

Then you can specify init, add and get_max_size and get_curr_size functions.

Init:

int_arr* arr_init(int_arr* ptr, size_t size)
{
    ptr = calloc(size, sizeof(int);
    ptr->max_size = size;
    ptr->curr_elem = 0;
    return ptr;
}

Add:

void add(int_arr* ptr, int value)
{
    if (ptr->curr_elem < ptr->max_size)
        ptr->arr_ptr[ptr->curr_elem++] = value;
    else
        printf("Max size reached!\n");
}

Get total size:

size_t get_total_size(int_arr* ptr) { return ptr->max_size; }

Get current size:

int get_curr_size(int_arr* ptr) { return ptr->curr_elem; }

Obviously replace int with your desired type.

Nobilis
  • 7,310
  • 1
  • 33
  • 67
  • Thanks. After a lot more searching the internet I realized what I was trying to do wasn't going to work. If the problem was more sophisticated the structure approach would be the way to go. Since all I am doing is filling the array with a list of primes I will just go with a length variable that gets set at the same time the array changes size. I will definitely keep this marked to refer back to as I am learning more about C. – user1362058 Jun 07 '13 at 17:39
  • Glad I was at least of some help, personally I find this a good way to provide some validation (such as boundary checking) on an otherwise very easy to misuse language :) Good luck! – Nobilis Jun 08 '13 at 02:06