6

How can I obtain the number of elements present in an integer array in C after the array is passed to a function? The following code doesn't work.

size=sizeof(array)/sizeof(array[0]);
tshepang
  • 12,111
  • 21
  • 91
  • 136
  • 5
    How is `array` declared? `int*` ?? Is `size` determined within a function and the `array` passed in as a parameter? – hmjd Apr 10 '13 at 15:17
  • 1
    `the following code doesn't work!!` Yes it does. Assuming that `array` is, in fact, an array. – Dan Moulding Apr 10 '13 at 15:18
  • try: `size=sizeof(array)/sizeof(int)` – karthikr Apr 10 '13 at 15:20
  • If you are uncapable to tell what "doesn't work" means, we are uncapable to help you. – glglgl Apr 10 '13 at 15:21
  • 1
    @hmjd----yes!! the size is determined within a function and the array is passed in as a parameter –  Apr 10 '13 at 15:27
  • 1
    Erroneous assumption. **You cannot pass arrays to functions in C** ... you can only pass their addresses. Naturally, addresses don't contain information about the length of the thing they address. – Jim Balter Apr 10 '13 at 16:18

3 Answers3

16

In C, you can only get the size of statically allocated arrays, i.e.

int array[10];
size = sizeof(array) / sizeof(int);

would give 10.

If your array is declared or passed as int* array, there is no way to determine its size, given this pointer only.

Elmar Peise
  • 14,014
  • 3
  • 21
  • 40
  • `int[10] array` -- That isn't C. And it isn't necessary for the array to be static. – Jim Balter Apr 10 '13 at 16:21
  • @JimBalter: Edited the C typo. For dynamically (as opposed to statically) declared arrays you cannot get the length. Or do I misunderstand you? – Elmar Peise Apr 10 '13 at 17:04
  • You misunderstand C, and what static means. – Jim Balter Apr 10 '13 at 18:21
  • Ah, now I understand the confusion. I was not referring to the `static` keyword but to [static memory allocation](http://en.wikipedia.org/wiki/Static_memory_allocation). – Elmar Peise Apr 10 '13 at 18:54
  • 2
    What you're failing to grasp is that local variables (including arrays), are dynamically allocated ... on the stack. Thus, whether array is statically or dynamically allocated depends upon whether it has file or block scope. (Of course, one can also dynamically allocate memory from the heap via malloc.) Not only can arrays be dynamically allocated on the stack, but as of C99 their size can be dynamic as well -- so-called Variable-Length Arrays (VLAs). – Jim Balter Apr 10 '13 at 21:57
  • ---code : static double const stepsArray[27] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 27}; -----code int stepsCount = sizeof(stepsArray) / sizeof(*stepsArray); //count = 27 ----code – Massmaker Feb 19 '18 at 12:23
11

You are most likely doing this inside the function to which you pass the array.
The array decays as pointer to first element So You cannot do so inside the called function.

Perform this calculation before calling the function and pass the size as an function argument.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • thank you....bt is there any way by which i can only pass the array into the function and find out the size inside that function? –  Apr 10 '13 at 15:27
  • @AnwitDaityari: No you cannot. What you show is the correct way to find the size of the array provided that when you perform this calculation `array` should be `int [x]`, When you pass array to a function it decays as pointer to the first element so when you perform this very same calculation inside the function body, the *type* of `array` is `int *` and `sizeof(int *)` gives you size of the pointer not the array. No there is no way you can perform this calculation within the function. You can, either pass the size explicitly or use a global variable or simply set a marker as last element. – Alok Save Apr 10 '13 at 15:30
  • @AnwitDaityari: Of all these, passing the size as an function argument is the most common, intuitive & correct way of achieving this. – Alok Save Apr 10 '13 at 15:31
1

You are going about it in the wrong way. I'll try to explain using a small code example. The explanation is in the code comments:

int array[100];
int size = sizeof(array) / sizeof(array[0]);  // size = 100, but we don't know how many has been assigned a value

// When an array is passed as a parameter it is always passed as a pointer.
// it doesn't matter if the parameter is declared as an array or as a pointer.
int getArraySize(int arr[100]) {  // This is the same as int getArraySize(int *arr) { 
  return sizeof(arr) / sizeof(arr[0]);  // This will always return 1
}

As you can see from the code above you shouldn't use sizeof to find how many elements there are in an array. The correct way to do it is to have one (or two) variables to keep track of the size.

const int MAXSIZE 100;
int array[MAXSIZE];
int size = 0; // In the beginning the array is empty.

addValue(array, &size, 32);   // Add the value 32 to the array

// size is now 1.

void addValue(int *arr, int *size, int value) {
    if (size < MAXSIZE) {
        arr[*size] = value;
        ++*size;
    } else {
        // Error! arr is already full!
    }
}
Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82