0

It's just a self-assessment question as a result of curiosity; so please cope with it.

I know that in C we pass things by value. In case of arrays the scenario is different. We cannot pass them by their values and if we don't; a demotion from array type to pointer type will occur. We need to pass the value of the pointer to the first element of the array or pointer to the array itself. Yeah, this is what people call as array decay. As, the original type information of array is lost while passing we cannot obtain the size information of the passed array in the called routine unless we pass by reference.

Have a look at the following code:

#include <stdio.h>
#include <stdlib.h>

void cal(int (*b)[3]) 
{
    printf("%d",sizeof(*b));
}

int main()
{
    int a[3] = { 2,3,4 };
    cal( &a ); 
}

Output:

12                                                            

Okay, we got what we want. But what I concluded is that the number of elements in the array are always required when you declare a pointer to a non-variable array like this :

int a[] = { 3, 4, 5 }; // declaring an array of size 3
int (*p)[3]; // declaring a pointer to an array with length 3
p = &a ; // assignment

So, you are implicitly passing the length of the array to called function and there always exist a different way of doing it by passing an extra length argument to the called function in the case when you don't pass the reference of the array.

P.S. You always need to pass the length whether you do in an explicit manner or in implicit one and it is just like hardcoding things.

Is there exist an other techniques for having the size information of the passed array in the called function or I'm hitting right on the money?
And, how will you deal with the dynamic ones'?

Mantosh Kumar
  • 5,659
  • 3
  • 24
  • 48
darxtrix
  • 2,032
  • 2
  • 23
  • 30

3 Answers3

3

Is there exist an other techniques for having the size information of the passed array in the called function or I'm hitting right on the money?

Yes you can. Put a sentinel value as a last element of array. Then use a loop on a passed array to get the size in that function.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • 1
    I cannot close this question as a dupe anymore, as the dupe-hammer only works once per question, but you can: http://stackoverflow.com/q/2404567/694576 ;-) – alk Jun 29 '14 at 13:57
2

The usual way is to declare the second parameter that will store the size of the array declared as the first parameter. In fact all standard C functions that deal with arrays declared such a way except functions that deal with character arrays that are considered as strings.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

Another technique is to use the first element of the array as the size. Of course, this usage is somewhat limited, e.g., an array of char can only store a value from CHAR_MIN to CHAR_MAX. You can also choose to always treat the value as unsigned, meaning any value from 0 to UCHAR_MAX is able to be stored, which is my preference.

Usage of this technique also means your array becomes 1-based instead of 0-based, and you're not able to store as many items as you could before (e.g., 65535 elements 1..65535 instead of 65536 elements 0..65535). It follows that an array with no elements would technically still take up space: the first element used to hold the length.

For example, consider a fixed-length string type where the first element is the length of the string. You can only have strings of length UCHAR_MAX, which is 255 on my system, due to the limit of values that may be stored in the first element. If the string is full, then the length stored in string[0] is 255 and the available elements is from string[1] to string[255].

An array of short int would have from 0 to USHRT_MAX elements, or from SHRT_MIN to SHRT_MAX if you prefer, though I still fail to see the usefulness of not treating it as an unsigned quantity. In my case, there could be up to 65535 elements, and they would all be addressed as arr[1], arr[2], ..., arr[ arr[0] - 1 ], arr[ arr[0] ].