2

I have this array in main.

int main()
{
    int list[] = {1,2,3};

    print_list(list);
}

According to the compiler warning the list is NULL terminated.

When I try to print the list I will print an extra element because sizeof(list) seems to be +1.

void print_list(const int *list)
{ 
    int i;
    for(i=0; i < sizeof(list); i++)
        printf(list[i]);
}

Should I run the for loop with sizeof(list) - 1 for any case that looks like this? Or should I check inside the loop for if(list[i] == NULL?

If my function was not making a pointer out of the list I could use: sizeof(list)/sizeof(list[0])

The function is turning the array into a pointer. So how can I get the accurate size?

It is true my question is very simple and trivial to some. I do believe I have shown enough effort though, please help.

[EDIT] Why isn't the size of an array parameter the same as within main? That is what I know already, it still doesn't answer my question so not a duplicate.

Community
  • 1
  • 1
  • 1
    In function `print_list()`, `list` is just a pointer. So you get the sizeof the pointer. – P.P Dec 11 '13 at 22:48
  • possible duplicate of [Sizeof an array in the C programming language?](http://stackoverflow.com/questions/1975128/sizeof-an-array-in-the-c-programming-language) – P.P Dec 11 '13 at 22:49
  • Yeah I am aware of what is going on with the pointer. So how do I get the accurate size? –  Dec 11 '13 at 22:57
  • Note that even if it were an array, your code would be wrong. `sizeof list` would be 12 on most machines, not 3. You have to do `sizeof array / sizeof *array` to get the number of elements in an array (unless it's an array of `char`). – Barmar Dec 11 '13 at 23:00
  • The answer to your question is simple: you **can't**. You have to pass the size as a parameter, or put an end marker in the array. – Barmar Dec 11 '13 at 23:06
  • The comment "The function is turning the array into a pointer" is a red flag. C arrays *are* pointers into memory, there is nothing to turn it into. "list" is just a name, not a C structure, so the function is not "making a pointer out of the list" - the variable "list" was always just a pointer, except in how your code used it. There is no way to get the size of the entire array from the pointer to the array. – Erik Johnson Dec 11 '13 at 23:44
  • 1
    @BlueMoon The other question is specifically a 'why' question, whereas this is a 'how' question. – mydoghasworms Dec 12 '13 at 05:34
  • @mydoghasworms exactly. –  Dec 12 '13 at 05:44

2 Answers2

3

sizeof returns the size in bytes of its parameter. sizeof(int*) will return, in most architectures, 4. It is impossible to get the size of the array using a pointer. Read this

Use this code:

main:

int main()
{
    int list[] = {1,2,3};

    print_list(list, sizeof(list) / sizeof(int));
}

print_list:

void print_list(const int *list, int size)
{ 
    int i;
    for(i=0; i < size; i++)
        printf(list[i]);
}
Community
  • 1
  • 1
Pacha
  • 1,438
  • 3
  • 23
  • 46
  • I personally like `#define LENGTHOF(a) (sizeof(a) / sizeof((a)[0]))` as it makes the rest of the code more legible when it is used frequently. [Ideone example](http://ideone.com/YXkGGV) – altendky Jan 07 '14 at 18:59
1

sizeof list reports the size of the variable list, which is a const int *, in modern computers it will probably be something like 4 or 8.

When you pass the array as a parameter to a function, it isn't an array anymore, the function receives a pointer.

In this link you can find the answer to your question. I recommend you to read this whole page (it is short), just to learn the difference between pointers and arrays.

Marco
  • 2,796
  • 19
  • 24
  • 2
    Might be 16 or 32 in the future?! – Ed Heal Dec 11 '13 at 22:49
  • Probably not, unless we ever design computers that can use the Internet as part of their main memory. But never say never -- Bill Gates supposedly opined that 640Kb would be enough for personal computers (although he claims he never said this, and no citation can be found). – Barmar Dec 11 '13 at 23:05