2

Can someone help me figure this out?

I think this is not possible... Unless i pass another parameter with the size? I'm looking to avoid it.

How could I do this?

int size(int *array){
    return (sizeof(array) / sizeof(int));
};

int main(int argc, const char * argv[])
{
    int array[] = {1,2,3,4};

    printf("%d", size(array) );

    return 0;
}

Thank you!

nmdias
  • 3,888
  • 5
  • 36
  • 59

1 Answers1

7

You can not do that as a function. You can do it as a macro though.

#define SIZEOF_ARRAY(array) (sizeof(array) / sizeof(*array))
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173