I'm trying to enumerate an array in C
(like how in python
you can use the #enumerate function).
static enumerate(const char *data[])
{
int i;
int stopLength = sizeof(data);
for(i=0; i < stopLength; i++)
{
printf("[%d] %s", i+1, data[i]);
}
}
int main(int argc, char *argv[])
{
char *friends[] = {"test", "test", "test"};
enumerate(friends);
}
However when I try to use this function I get the following error:
utilis.c: In function ‘int enumerate(char**)’:
utilis.c:58:33: warning: ‘sizeof’ on array function parameter ‘data’ will return size of ‘char**’ [-Wsizeof-array-argument]
int stopLength = sizeof(data);
^
utilis.c:55:29: note: declared here
static enumerate(char *data[])
^
utilis.c: In function ‘int main(int, char**)’:
utilis.c:78:46: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
char *friends[] = {"test", "test", "Test"};
^
utilis.c:78:46: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
utilis.c:78:46: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
I understand what the error is telling me, but I don't understand how to implement what I want to do. In python
I could do something like this:
friends = ["test", "test", "test"]
for i, f in enumerate(friends, start=1):
print "[{}] {}".format(i, f)
# [1] test
# [2] test
# [3] test
How can I get my C
function to behave as I want it to?