1

If I wrote it in the following way, I return the result as 1. Don't understand why? The expected answer is 5.

    int main()
    {
      const char* list_of_filename[] = {"One","Two","Three","Four","Five"};

      printf("%d", countFiles(list_of_filename));

      return 0;
    }

    int countFiles(const char* list_of_filename[])
    {
      return sizeof(list_of_filename) / sizeof(list_of_filename[0]);
    }
Fusionmate
  • 679
  • 1
  • 8
  • 18

4 Answers4

4

sizeof for an array only works in the scope where you declare the array.

  const char* list_of_filename[] = {"One","Two","Three","Four","Five"};
  printf("%d", sizeof(list_of_filenames) / sizeof(list_of_filenames[0]));

In C, you can not pass arrays. So:

int countFiles(const char* list_of_filename[])

is exactly the same as:

int countFiles(const char **list_of_filename)

So you're comparing the sizes of two pointers. The size of const char * and const char ** are the same on your system.

This answer (by Michael Burr, but originally from Chromium) provides a macro that does the array size calculation, and errors on some invalid constructs like this (but not this one)

Community
  • 1
  • 1
Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
2

As a function argument,

const char* list_of_filename[]

actually means:

const char** list_of_filename.

Therefore what you're in fact calculating is:

sizeof(char**) / sizeof(char*)

char** and char* are both just pointers, and since all pointers generally have the same size (at least in this case, for you, these two do):

x / x
= 1


EDIT: When passing an array to a function in C, you'll generally either want to pass the size of the array or terminate your array with a specific value in order to be able to determine the length of it.

AusCBloke
  • 18,014
  • 6
  • 40
  • 44
1

From the C standard (C11 6.3.2.1 Lvalues, arrays, and function designators, para 3):

Except when it is the operand of the sizeof operator, the _Alignof operator, or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue.

In other words, when you pass that array to a function, it becomes a pointer to the first element of the array and its size is therefore the size of a pointer.

If you want to treat it as an array within the function, you need to explicitly pass in the size information with something like:

void printArray (int *arr, size_t sz) {
    for (size_t i = 0; i < sz; i++)
        print ("%d ", arr[i]);
    puts ("");
}
:
int xyzzy[] = {3,1,3,1,5,9,2,6,5,3,5,8,9};
printArray (xyzzy, sizeof (xyzzy) / sizeof (*xyzzy));
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 1
    xyzzy? You are in a maze of twisty little array indices, all alike. :-) – Adam Liss Apr 05 '12 at 03:59
  • @Adam, congrats on being the first to recognise the reference despite me having been using xyzzy and plugh for several months now. As a prize, you get a random upvoe to three of your answers, provided I can find three that are worthy :-) – paxdiablo Apr 05 '12 at 04:01
  • 2
    What, with your bare hands? Plugh! (I guess we're both showing our age, huh?) – Adam Liss Apr 05 '12 at 04:03
  • I just looked that up, that's one seriously old game. – AusCBloke Apr 05 '12 at 04:04
0

C Arrays and Pointers are not the same!

One of the first things that novice C programmers often hear is that "arrays are the same as pointers.". Unfortunately, this is a dangerous half-truth. The ANSI C Standard paragraph 6.5.4.2 recommends that you

Note the distinction between the declarations:

extern int *x;
extern int y[];

The first declares x to be a pointer to int; the second declares y to be an array of int of unspecified size (an incomplete type), the storage for which is defined elsewhere.

The standard doesn't go into the matter in any greater detail than that.

So when you try to get sizeof from array it returns size of the whole array in bytes (sum of objects' sizes), when you try to get sizeof of pointer - it returns size of pointer in bytes. Size of pointers it is always constant value on one platform. Generally it is 4 or 8 bytes.

Remember this :)

fasked
  • 3,555
  • 1
  • 19
  • 36