# Integer array length

**URL:** <https://community.unix.com/t/integer-array-length/334008>\
**Category:** Programming\
**Created:** [September 8, 2013, 2:53am UTC](https://community.unix.com/t/integer-array-length/334008 "2013-09-08T02:53:42Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![yifangt](https://community.unix.com/letter_avatar/yifangt/32/6_449c533f586939b6e5f246632a46421f.png) [@yifangt](https://community.unix.com/u/yifangt)\
**Post date:** [September 8, 2013, 2:53am UTC](https://community.unix.com/t/integer-array-length/334008/1 "2013-09-08T02:53:42Z")

</div>

Hello;  
When I wrote a function to print out an array,

```nohighlight
void p_array(int arr[])
 {
   int i;
    int size = sizeof(arr) / sizeof(int);
 // int size = sizeof (arr) / sizeof (arr[0]);
     for (i = 0; i < size; i++)
         printf("%d ", arr);
     printf("\n");
 }

```

I could only print out the first two elements,( sometimes 8 out of 10 elements). What did I miss?  
The problem seems to be the part

```nohighlight
size = sizeof arr/sizeof(int);
size = sizeof arr /sizeof arr[0];

```

My understanding is (copied from stackoverflow):  
sizeof returns the size in memory of the passed element. By dividing the size of an array by a single element size, you get the elements count. On the other hand, when counting elements in an array: sizeof(a) / sizeof(a[0]) will still work as well.  
But my code does not! I saw this type online quite lot, but mine just does not work (Ubuntu 12.10, 64\_bits kernel). Could anyone explain what is wrong with my code? Thanks a lot!

---

<div class="post-metadata">

**Author:** ![Scott](https://community.unix.com/letter_avatar/scott/32/6_449c533f586939b6e5f246632a46421f.png) [@Scott](https://community.unix.com/u/Scott)\
**Post date:** [September 8, 2013, 8:37am UTC](https://community.unix.com/t/integer-array-length/334008/2 "2013-09-08T08:37:52Z")

</div>

Your array is passed as a pointer. There's no way for your function to know the size of the array, unless you pass that too.

i.e.

```nohighlight
void p_array(int arr[], int size) {
 ...
}

```

A 64-bit pointer (8 bytes) divided by an integer of 4 bytes = 2, which is why you're seeing only two elements.

---

<div class="post-metadata">

**Author:** ![kg\_gaurav](https://community.unix.com/letter_avatar/kg_gaurav/32/6_449c533f586939b6e5f246632a46421f.png) [@kg\_gaurav](https://community.unix.com/u/kg_gaurav)\
**Post date:** [September 8, 2013, 8:48am UTC](https://community.unix.com/t/integer-array-length/334008/3 "2013-09-08T08:48:36Z")

</div>

i'll try your code on my machine once i'll get to home but i would rather like to do this by

```nohighlight
void p_array(int arr[])
 {
       int *a = arr;
 // int size = sizeof (arr) / sizeof (arr[0]);
     while(a!=NULL)
        { printf("%d ", *a);a++}

     printf("\n");
 }

```

---

<div class="post-metadata">

**Author:** ![achenle](https://community.unix.com/letter_avatar/achenle/32/6_449c533f586939b6e5f246632a46421f.png) [@achenle](https://community.unix.com/u/achenle)\
**Post date:** [September 8, 2013, 8:53am UTC](https://community.unix.com/t/integer-array-length/334008/4 "2013-09-08T08:53:16Z")

</div>

> [@kg\_gaurav](#):
>
> i'll try your code on my machine once i'll get to home but i would rather like to do this by
> 
> void p\_array(int arr)  
> {  
> int \*a = arr;  
> // int size = sizeof (arr) / sizeof (arr[0]);  
> while(a!=NULL)  
> { printf("%d ", \*a);a++}
> 
> printf("\n");  
> }

You can't.

---

<div class="post-metadata">

**Author:** ![yifangt](https://community.unix.com/letter_avatar/yifangt/32/6_449c533f586939b6e5f246632a46421f.png) [@yifangt](https://community.unix.com/u/yifangt)\
**Post date:** [September 8, 2013, 10:55am UTC](https://community.unix.com/t/integer-array-length/334008/5 "2013-09-08T10:55:48Z")

</div>

Thanks everybody!  
Scott, what I want is to let my function by itself get the size of the array, especially when the array size is unknown.  
Your explanation about "2" is what I thought, but how come sometime it print 8 elements of the array?  
I feel it is related to array and pointer/address, but I could not figure it out myself, especially when google gives many discussions, for example, [this one](http://stackoverflow.com/questions/37538/how-do-i-determine-the-size-of-my-array-in-c). but I am still unclear the problem with my code. Thanks a lot again!

---

<div class="post-metadata">

**Author:** ![alister](https://community.unix.com/letter_avatar/alister/32/6_449c533f586939b6e5f246632a46421f.png) [@alister](https://community.unix.com/u/alister)\
**Post date:** [September 8, 2013, 11:41am UTC](https://community.unix.com/t/integer-array-length/334008/6 "2013-09-08T11:41:27Z")

</div>

> [@yifangt](#):
>
> Your explanation about "2" is what I thought, but how come sometime it print 8 elements of the array?

Show us the code that prints 8 elements. An entire program, not just the printing function. If it is an extremely long program, eliminate all non-essentials and construct a minimal case that still prints out 8 elements. Perhaps then, we can provide accurate feedback.

Given what you've shown us, I believe Scott's explanation is as good as it gets. For more info regarding his point, refer to the [comp.lang.c FAQ - Question 6.4](http://c-faq.com/aryptr/aryptrparam.html).

Regards,  
Alister

---

<div class="post-metadata">

**Author:** ![yifangt](https://community.unix.com/letter_avatar/yifangt/32/6_449c533f586939b6e5f246632a46421f.png) [@yifangt](https://community.unix.com/u/yifangt)\
**Post date:** [September 8, 2013, 3:57pm UTC](https://community.unix.com/t/integer-array-length/334008/7 "2013-09-08T15:57:32Z")

</div>

Here is my code, which is for quick sort I am trying to understand each step of the algorithm.

```nohighlight
1 #include<stdio.h>

3 void qsort(int[], int, int);
4 void p_array(int[]);

6 int main()
{
8 int i;
9 int array[] = { 55, 66, 32, 12, 9, 73, 2, 4, 37, 36 };
10 int len = sizeof(array) / sizeof(int);
11 for (i = 0; i < len; i++)
12 printf("%d ", array);
    printf("\n");
14 p_array(array);

    qsort(array, 0, len - 1);
17 for (i = 0; i < len; i++)
18 printf("%d ", array);
    printf("\n");
    return 0;

}

 23 void qsort(int a[], int left, int right)
{
    int pivot, l, r, temp;

    l = left;
    r = right;
    pivot = a[(left + right) / 2];

    while (l < r) {
    while (a[l] < pivot) {
        ++l;
34 p_array(a);
    }
    while (a[r] > pivot) {
        --r;
38 p_array(a);
    }
    if (l >= r)
        break;
    temp = a[l];
    a[l] = a[r];
    a[r] = temp;
    ++l;
    --r;
47 p_array(a);
    }
    if (l == r) {
    l++;
51 p_array(a);
   }
    if (left < r) {
    qsort(a, left, l - 1);
55 p_array(a);
    }
    if (l < right) {
    qsort(a, r + 1, right);
59 p_array(a);
    }
60 p_array(a);
}

63 void p_array(int arr[])
{
    int i;
66 int size = sizeof(arr) / sizeof(int);
// int size = sizeof (arr) / sizeof (arr[0]);
    for (i = 0; i < size; i++)
    printf("%d ", arr);
    printf("\n");
}

```

I labeled the p\_array call for convenience when you point out the problem. I could not understand in the main function line 10, which works fine, but not  
line 66.  
Output:

```nohighlight
55 66 32 12 9 73 2 4 37 36 // this from line 10 ~ 12, which is correct
55 66 
2 4 
...
2 4 
2 4 9 12 32 36 37 55 66 73 // this from line 17 ~ 18 which is also correct 

```

Why?  
By the way, this is NOT an assignment from school, but my self-study. Appreciate any input. Thanks a lot!

---

<div class="post-metadata">

**Author:** ![Scott](https://community.unix.com/letter_avatar/scott/32/6_449c533f586939b6e5f246632a46421f.png) [@Scott](https://community.unix.com/u/Scott)\
**Post date:** [September 9, 2013, 10:06am UTC](https://community.unix.com/t/integer-array-length/334008/8 "2013-09-09T10:06:59Z")

</div>

For the same reason. The two numbers displayed on each call of p\_array() stem from the fact that a[] is an 8-byte pointer and sizeof(int) is 4 bytes, and 8/4 = 2.

So, it's displaying the first two elements of the array as you're sorting it in qsort().

Note that you passed len to qsort(). Why can't you pass it to q\_array()?

---

<div class="post-metadata">

**Author:** ![yifangt](https://community.unix.com/letter_avatar/yifangt/32/6_449c533f586939b6e5f246632a46421f.png) [@yifangt](https://community.unix.com/u/yifangt)\
**Post date:** [September 9, 2013, 11:00am UTC](https://community.unix.com/t/integer-array-length/334008/9 "2013-09-09T11:00:41Z")

</div>

Thanks Scott!  
Yes, I did pass len to p\_array, but did not make any difference. Here my focus is about the calculation of the array length.  
I could not understand why the calculation of array length in the main function is fine (line 10), but not in the p\_array function (line 66). Thanks!

---

<div class="post-metadata">

**Author:** ![Scott](https://community.unix.com/letter_avatar/scott/32/6_449c533f586939b6e5f246632a46421f.png) [@Scott](https://community.unix.com/u/Scott)\
**Post date:** [September 9, 2013, 11:04am UTC](https://community.unix.com/t/integer-array-length/334008/10 "2013-09-09T11:04:39Z")

</div>

In the main() function it's an array. In the p\_array() function it's a pointer to the first element of the array.

I didn't see where you passed len to p\_array(), or how it was used there, so I can't comment on it.

---

<div class="post-metadata">

**Author:** ![yifangt](https://community.unix.com/letter_avatar/yifangt/32/6_449c533f586939b6e5f246632a46421f.png) [@yifangt](https://community.unix.com/u/yifangt)\
**Post date:** [September 9, 2013, 11:15am UTC](https://community.unix.com/t/integer-array-length/334008/11 "2013-09-09T11:15:14Z")

</div>

Sorry I meant I tried passing len to the p\_array function like this:

```nohighlight
#include<stdio.h>
void qsort(int[], int, int);
void p_array(int[], int);

int main()
{
    int i;
    int array[] = { 55, 66, 32, 12, 9, 73, 2, 4, 37, 36 };
    int len = sizeof(array) / sizeof(int);
    for (i = 0; i < len; i++)
        printf("%d ", array);
    printf("\n");
    p_array(array, len);
return 0;
}

void p_array(int arr[], int size)
{
    int i;
    size = sizeof(arr) / sizeof(int);
// int size = sizeof (arr) / sizeof (arr[0]);
    for (i = 0; i < size; i++)
        printf("%d ", arr);
    printf("\n");
}

```

output:

```nohighlight
55 66 32 12 9 73 2 4 37 36 
55 66 

```

I did not catch the part you said it is an array in the main() but pointer in the p\_array(). Seems that's the part I missed. Thanks a lot!

---

<div class="post-metadata">

**Author:** ![Scott](https://community.unix.com/letter_avatar/scott/32/6_449c533f586939b6e5f246632a46421f.png) [@Scott](https://community.unix.com/u/Scott)\
**Post date:** [September 9, 2013, 11:20am UTC](https://community.unix.com/t/integer-array-length/334008/12 "2013-09-09T11:20:26Z")

</div>

You still have this, which will override the passed variable:

```nohighlight
    size = sizeof(arr) / sizeof(int);

```

Also, as you originally passed len - 1 to qsort(), you'll need to modify the for-loop in p\_array to:

```nohighlight
    for (i = 0; i <= size; i++)

```

---

<div class="post-metadata">

**Author:** ![yifangt](https://community.unix.com/letter_avatar/yifangt/32/6_449c533f586939b6e5f246632a46421f.png) [@yifangt](https://community.unix.com/u/yifangt)\
**Post date:** [September 9, 2013, 11:26am UTC](https://community.unix.com/t/integer-array-length/334008/13 "2013-09-09T11:26:53Z")

</div>

Thanks again! That bug only make small difference. Output:

```nohighlight
55 66 32 12 9 73 2 4 37 36 
55 66 32 

```

I feel understand pointer and array in C (not fluent to use, though!). Could you explain why the two calculations differ?

```nohighlight
int len = sizeof(array) / sizeof(int); //10
int size = sizeof(arr) / sizeof(int); //2

```

Thanks.

---

<div class="post-metadata">

**Author:** ![Scott](https://community.unix.com/letter_avatar/scott/32/6_449c533f586939b6e5f246632a46421f.png) [@Scott](https://community.unix.com/u/Scott)\
**Post date:** [September 9, 2013, 12:08pm UTC](https://community.unix.com/t/integer-array-length/334008/14 "2013-09-09T12:08:24Z")

</div>

I'm not sure where you get 8 from - you'll have to show the code (in context) that produces that number, but passing the length works perfectly fine:

```nohighlight
# cat main.c
#include <stdio.h>

int main( int argc, char **argv ) {
  int array[10] = { 3, 6, 9, 12, 15, 18, 21, 24, 27, 30 };
  int i, len = sizeof( array ) / sizeof( int );
  printf( "In main, length of array: %d\n", len );
  printf( "In main, array elements:" );
  for( i = 0; i < len; i++ )
    printf( " %d", array );

  printf( "\n" );
  printf( "\n" );

  showarr_1( array );
  showarr_2( array, 10 );

  return 0;
}
 
int showarr_1( int array[] ) {
  int len = sizeof( array ) / sizeof( int );
  printf( "In showarr_1, length of array: %d (bogus number!)\n", len );
  printf( "\n" );

  return 0;
}
 
int showarr_2( int array[], int len ) {
  int i;
  printf( "In showarr_2, length of array: %d\n", len );
  printf( "In showarr_2, array elements:" );
  for( i = 0; i < len; i++ )
    printf( " %d", array );

  printf( "\n" );

  return 0;
}

# ./main
In main, length of array: 10
In main, array elements: 3 6 9 12 15 18 21 24 27 30

In showarr_1, length of array: 2 (bogus number!)

In showarr_2, length of array: 10
In showarr_2, array elements: 3 6 9 12 15 18 21 24 27 30

```

edit: you updated your post, to change 8 to 10. It should be clear now why the numbers are different. As previously said: one is an array, the other is a pointer (to the array).

---

<div class="post-metadata">

**Author:** ![yifangt](https://community.unix.com/letter_avatar/yifangt/32/6_449c533f586939b6e5f246632a46421f.png) [@yifangt](https://community.unix.com/u/yifangt)\
**Post date:** [September 9, 2013, 12:30pm UTC](https://community.unix.com/t/integer-array-length/334008/15 "2013-09-09T12:30:02Z")

</div>

Two things still unclear to me:  
1) Why the two exact formulas in main() and in showarray\_1() get two different number 10 vs 2? That's the part bugs me very much.  
2) In your showarray\_2(int array[], int len), the local variable len is not calculated. In the main() call, len was called from the main(), that's I want to avoid in the cases when the array length is unknown. I can't provide an example yet.....maybe I am wrong!  
It seems I really need read more to get thru this point.  
Thank you so much Scott!

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_449c533f586939b6e5f246632a46421f.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [September 9, 2013, 12:36pm UTC](https://community.unix.com/t/integer-array-length/334008/16 "2013-09-09T12:36:02Z")

</div>

Try this:

```nohighlight
int arr[32];
int *parr=arr;

printf("sizeof(arr)=%d\n", sizeof(arr));
printf("sizeof(parr)=%d\n", sizeof(parr));

```

The only time the compiler knows the exact size of the array is when you use it directly. If you pass it around, it becomes a pointer to something of unknown size.

It only knows when you use it directly, because sizeof() is hardcoded. Its value is fixed in stone before the program even runs. You cannot use it in a dynamic way.

---

<div class="post-metadata">

**Author:** ![yifangt](https://community.unix.com/letter_avatar/yifangt/32/6_449c533f586939b6e5f246632a46421f.png) [@yifangt](https://community.unix.com/u/yifangt)\
**Post date:** [September 9, 2013, 12:39pm UTC](https://community.unix.com/t/integer-array-length/334008/17 "2013-09-09T12:39:27Z")

</div>

the compiler knows the exact size of the array is when you use it directly. Now I got it! Thanks a lot, and thank you, too, Scott!

---

<div class="post-metadata">

**Author:** ![jim\_mcnamara](https://community.unix.com/letter_avatar/jim_mcnamara/32/6_449c533f586939b6e5f246632a46421f.png) [@jim\_mcnamara](https://community.unix.com/u/jim_mcnamara)\
**Post date:** [September 9, 2013, 12:49pm UTC](https://community.unix.com/t/integer-array-length/334008/18 "2013-09-09T12:49:52Z")

</div>

I'll try.

When a function is called the argument( parameters) for the function are pushed onto the stack. When C was first developed, memory space, and therefore also available stack space, were limited.

When a function is called it is more efficient to pass as a parameter the address of an object than the whole object. It is also faster. They decided that since arrays could be arbitrarily large, it was better to pass the address of the array.

In a 32 bit environment addresses are 32 bits long. int datatypes are usually 32 bit as well. You are confusing:

1. the address of your array with the physical memory object of the array itself:  
The address is 32 bits, each element of the array is 32 bits because they are the  
the int datatype.
2. There is nothing in the address of an array or the pointer to the array to tell the  
program how big the array object is.
3. Because pointers are 32 bits you can print them just like an integer, using "%d".  
Which is wrong. To print a pointer use "%p".
4. sizeof() gives you the number of bytes of a datatype, all 32 bit pointers are 4 bytes.

```nohighlight
void foo(int *arr)
{
    printf("%d ", arr); //WRONG
    printf("%p\n", arr); // correct
    printf("%u %u %u\n", sizeof(arr), sizeof(int *), sizeof(int)); // ALL are the same size
}

int main()
{
    int arr[]={1,2,3,4,5,6,7,8,9,10};
    foo(arr);
    printf("size of arr = %u\n", sizeof(arr));
    return 0;
}

```

---

<div class="post-metadata">

**Author:** ![Scott](https://community.unix.com/letter_avatar/scott/32/6_449c533f586939b6e5f246632a46421f.png) [@Scott](https://community.unix.com/u/Scott)\
**Post date:** [September 9, 2013, 12:55pm UTC](https://community.unix.com/t/integer-array-length/334008/19 "2013-09-09T12:55:55Z")

</div>

As I haven't done any C programming for about 15 years, I'm glad you guys showed up to add better explanations to the matter.

Thanks 🙂

---

<div class="post-metadata">

**Author:** ![yifangt](https://community.unix.com/letter_avatar/yifangt/32/6_449c533f586939b6e5f246632a46421f.png) [@yifangt](https://community.unix.com/u/yifangt)\
**Post date:** [September 9, 2013, 1:07pm UTC](https://community.unix.com/t/integer-array-length/334008/20 "2013-09-09T13:07:34Z")

</div>

No worry! I should have dove into C 15 years ago, and now I realize C is so important for programming!
