1

EDIT: I believe this question is different from How do I determine the size of my array in C?, because that link discusses how to use sizeof(nlist)) / sizeof(nlist[0]) to determine the number of items in an array.

My question is asking why that stops working after the array has been passed to a function.

===

I'm new to ansi C, coming from Python.

I have a function that parses an int array. The iteration through the array is dependent on sizeof(nlist)) / sizeof(nlist[0]) to determine the size of the array.

However, while this works in main(), it fails when the array is passed to a function.

In main file

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "arrayTools.h"

int main() {

    // Fill with data for testing
    int nlist[500];
    for (int i=1; i<501; i++ ) {
        nlist[i] = i;
    }

    // This successfully iterates through all 500 items
    for( size_t i = 1; i <= (sizeof(nlist)) / sizeof(nlist[0]); i++)
    {   
        printf(isItemInIntArray(i, nlist) ? "true\n" : "false\n");
    }

arrayTools.h

#include <stdbool.h>
#include <stdlib.h>

bool isItemInIntArray(int value, int arr[]){
    // This only iterates twice (i = 1, then i = 2) and then ends
    for( size_t i = 1; i <= (sizeof(arr)) / sizeof(arr[0]); i++) {
        if (value == arr[i]) { return true; }
    }
    return false;
}
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
RightmireM
  • 2,381
  • 2
  • 24
  • 42
  • Are you aware that sizeof is not the same as len, right? – norok2 Oct 24 '18 at 17:42
  • 2
    See https://stackoverflow.com/questions/1461432/what-is-array-decaying – Nelfeal Oct 24 '18 at 17:43
  • @dbush I ask that you remove the duplicate status, since that link doesn't address my problem. It discusses using ` (sizeof(nlist)) / sizeof(nlist[0])` to determine the number of elements in an array. **However** my question is why does `(sizeof(nlist)) / sizeof(nlist[0])` **STOP working after the array has been passed to a function**. Thanks! – RightmireM Oct 24 '18 at 20:13
  • @RightmireM The second answer in the post addresses your issue. You need to explicitly send the size. – dbush Oct 24 '18 at 20:15
  • @norok2 "Are you aware that sizeof is not the same as len". Sort of. I know that, functionally, the `(sizeof(nlist)) / sizeof(nlist[0])` pragmatically returns the `len` of the array (in `main()`). Googling, as well as the link above, show that there is no `len` command in `ansi c`. Is there a better way to determine the number of items in an array? – RightmireM Oct 24 '18 at 20:18
  • @dbush Thanks for the response. In fact, I had seen that technique - but was trying to avoid it - if possible. The link does do a nice job of explaining the problem. As I mentioned, I'm new to `c`. Is there no way to avoid sending the length as a parameter? – RightmireM Oct 24 '18 at 20:20
  • @RightmireM There is not. An array decays into a pointer to the first element when passed to a function, so inside the function `sizeof` gives you the size of a pointer. – dbush Oct 24 '18 at 20:23
  • 2
    See [this answer](https://stackoverflow.com/a/10349610/1505939) on the linked thread – M.M Oct 24 '18 at 20:58
  • 2
    Detail: "why that stops working after the array has been passed to a function" --> In `isItemInIntArray(int value, int arr[])`, `int arr[]` is not an _array_, but a pointer. Thus `sizeof(arr)` is the size of a pointer. There are alternatives to sending the array size as a separate argument, yet they likely do not meet your coding goals. – chux - Reinstate Monica Oct 27 '18 at 22:48

0 Answers0