0

I have the following function in C

int func(char* param1[], int param2[])
{
//Want to calculate size of param1 array
}

I tried

n = sizeof(param1)/sizeof(char*);

but this doesnt give me the correct answer.

jacksparrow007
  • 1,298
  • 4
  • 19
  • 30

4 Answers4

3

Note that the function prototype

int func(char* param1[], int param2[]);

is equivalent to

int func(char **param1, int *param2);

This means the function parameters param1 and param2 are pointers, not arrays. Pointers and arrays are different types.

sizeof(param1) / sizeof(char*);
// equivalent to
sizeof(char **) / sizeof(char *)  // always 1

The above expression always evaluates to 1 because size of all pointer types is the same (except for a function pointer on which sizeof operator may not be applied).

That's because you cannot pass an array to a function. What's actually gets passed is a pointer to the first element of the array. The pointer has no size information of the array passed to the function. Therefore, you must explicitly pass the array lengths to your function. It should have the prototype

int func(char *param1[], int param2[], int len_param1, int len_param2);
ajay
  • 9,402
  • 8
  • 44
  • 71
1

There are two ways of doing this:

  1. Simplest and most obvious, pass the length in the function argument
  2. Have a NULL at the end of the array (NULL-terminator):

char arr[] = { "what", "so", "ever", NULL }; Then loop:

int i;
for (i = 0; arr[i] != NULL; i++)
   ...

However, if you're passing an array like the example above to that function (a static one), just pass the length as an argument by using same logic...

func(arr, sizeof(arr) / sizeof(arr[0]);

0

C isn't smart enough to know the size of an array at runtime. It can only tell you the size of a data type, which is determined at compile-time.

The solution to this is to add a size parameter to the function, like this:

int func(char* param1[], int param2[], int n)

or to use a null-terminated array, so that you can use a loop to iterate through the array:

int func(char* param1[], int param2[]){
    int size;
    for(size = 0; param1[size] != NULL; size++);
    ...

The point is, an array in C is just a block of memory that you can happen to treat as a bunch of variables next to each other. There's no built-in way to figure out how many variables are in the block, since there's no built-in way to mark the beginning or end of the block.

millinon
  • 1,528
  • 1
  • 20
  • 31
  • For completeness, in C++ the size of the array is part of the type (that part may be true of C too), so it is possible to extract this *if you have an array*. Once you're dealing with a pointer (as happens in the many situations where arrays decay to pointers) all size information is lost. – juanchopanza Apr 24 '14 at 06:32
  • Could you give an example of a way to extract the size of an arbitrary array in C / C++? As far as I can tell, there isn't any way to determine an arbitrary array's length during runtime, even in the context in which it is created. – millinon Apr 24 '14 at 07:09
  • There is no `C/C++`. In C++, `template constexpr size(T(&)[N]) {return N;}` would do the trick. This size is known compile time. – juanchopanza Apr 24 '14 at 07:12
  • That's still a compile-time construct, though, similar to sizeof(). This, and similar questions, all seem to be about run-time size deduction. – millinon Apr 24 '14 at 07:26
  • Yes, and all types sizes are known at compile time. – juanchopanza Apr 24 '14 at 07:39
  • Again, this question (and the similar ones) tend to be about the size of the aggregate structure, not the size of the data type. When someone asks how to find the size of an array, it is not helpful to tell them how to tell the size of the array's type. – millinon Apr 24 '14 at 15:28
0
char* param1[]

will make param a pointer of type char ** so the result of

n = sizeof(param1)/sizeof(char*);

is sizeof pointer by size of pointer i.e. 1. and its not size of array

LearningC
  • 3,182
  • 1
  • 12
  • 19