-4

I try to create a program in which in the main I declare an array and load it elements. There is also a function that counts the number of elements in the array but when I want to show the result, a memory address appears instead of the number of elements and the warning above.

The code:

#include <stdio.h>
#include <conio.h>

int countArrayElement(int arr[]);

int main()
{
    int intMyArray[]= {1,2,3,4,6,7};
    countArrayElement(intMyArray);
    printf("The quantity of elements in the array is %d",&countArrayElement);
    getch();
    return 0;
}

int countArrayElement(int arr[])
{
    int count = 0;
    count  = sizeof (arr)/sizeof(int);  // wont work at all, arr is just the first element
    return count;
}
Alejandro Caro
  • 998
  • 1
  • 10
  • 22
  • `printf("The quantity of elements in the array is %d",countArrayElement());` – Retired Ninja Aug 25 '19 at 02:20
  • Don't work the suggest corrections – Alejandro Caro Aug 25 '19 at 02:25
  • 1
    **Note** that `sizeof( arr )` inside your function will give the size of an `int *` (because that is what `int arr[]` will resolve to), **not** the size of the array. `sizeof` is a compile-time operation. – DevSolar Aug 25 '19 at 02:32
  • ...with the exception of Variable Length Arrays, as chux noted below, but that would not change things -- you cannot determine the size of an array this way in a function. – DevSolar Aug 25 '19 at 06:16

1 Answers1

2

The error message says it all. In format strings, %d denotes an integer value whereas you give it a pointer to a function. You want to change the print to argument to actually be the result of the function call:

printf("The quantity of elements in the array is %d", countArrayElement(intsMyArray));

Retrieving the number of elements in array in C is not as straightforward as other languages.

Michael
  • 2,673
  • 1
  • 16
  • 27
  • Give me error: lvalue required as unary '&' operand – Alejandro Caro Aug 25 '19 at 02:28
  • @AlejandroCaro I updated the post - don't include the ampersand in the argument to the `print` call. – Michael Aug 25 '19 at 02:29
  • Doing the specified gives me 1 – Alejandro Caro Aug 25 '19 at 02:31
  • @AlejandroCaro did you look at the post I linked to? – Michael Aug 25 '19 at 02:36
  • @AlejandroCaro: See my comment above. That's as expected. Giving `int[]` as parameter to a function is just a fancy way of giving `int *`. Consider: You *don't* want to pass (and, thus, *copy*) the whole array... Also, `sizeof` is a *compile*-time operator, not a run-time function! – DevSolar Aug 25 '19 at 02:41
  • @DevSolar Note: `sizeof` can be computed at [run time](https://stackoverflow.com/questions/10078283/how-sizeofarray-works-at-runtime) – chux - Reinstate Monica Aug 25 '19 at 05:21
  • @chux: Yes, for VLA's it is evaluated at runtime, but only in the scope it's actually visible as a VLA (i.e., not in a called function, where it degraded to pointer to base type). And in the scope the VLA is visible, I "know" its size already. Or have I misunderstood something there? – DevSolar Aug 25 '19 at 05:29
  • @DevSolar What appears amiss is the "sizeof is ... not a run-time function!"", nothing more. – chux - Reinstate Monica Aug 25 '19 at 05:53