0

I'm trying to enumerate over an array in C, much like pythons enumerate method.

static void enumerate(char *choices[])
{
    int i;
    int length = sizeof(choices)/sizeof(*choices);

    for (i = 0; i < length; i++)
    {
        printf("[%d] %s\n", i, choices[i]);
    }
}

How I'd expect this to work is, create an array (or list): char *test[] = {"test", "testing}; then pass that array (or list) to the function: enumerate(test); and the expected output should be:

[0] test
[1] testing

However when I try this, the output I'm getting is just the first item in the array:

[0] test 

What am I doing wrong to where this will not work as I'd expect it to?

ekultek
  • 9
  • 2
  • 3
    That's a classical beginner error. `sizeof` can't know the size of the array when given as a pointer. – markand Apr 10 '17 at 12:03
  • Another classical beginner error is not printffing out the value of length as a debug measure. – ThingyWotsit Apr 10 '17 at 12:03
  • The implicit conversion of the type of `sizeof` to `int` is also a bit naughty. – Bathsheba Apr 10 '17 at 12:05
  • 1
    There is no array in your function! Do some research about this statement and think about the implications. (also: `enumerate` is not a _method_, but a _function_ and your function is very different from that Python function) – too honest for this site Apr 10 '17 at 12:11

0 Answers0