1

I have the following:

typedef unsigned char BYTE;

size_t arrMod (BYTE ARR[3])
{
    ARR[1] = 255;
    return sizeof(ARR);
}

int main()
{
    BYTE arr[3];
    size_t s = arrMod(arr);

    fprintf(stdout,
    "Size = %i\nARR[0] = %i\nARR[1] = %i\nARR[2] = %i",
    s, arr[0], arr[1], arr[2]);

    return 1;
}

And the output is:

Size = 12
ARR[0] = 0
ARR[1] = 255
ARR[2] = 0

I know that i can't give it sizeof(ARR) to get the wished 3 bytes, because the array decays into a pointer. I am basically receiving the pointer size. How can i get arr's size (3) as a return value from the function?

Edenia
  • 2,312
  • 1
  • 16
  • 33
  • 1
    write `return 3;` or `size_t arrMod (BYTE (*ARR)[3])`, call `arrMod(&arr);`, `return sizeof(*ARR)` – BLUEPIXY Aug 15 '14 at 03:26
  • If you want the arg to be passed by value, put the `ARR[3]` in a `struct`. – Déjà vu Aug 15 '14 at 03:27
  • @BLUEPIXY I can't just return 3. What if i declare larger array? I have to edit the function as well.. – Edenia Aug 15 '14 at 03:27
  • you could make use of defines – lordkain Aug 15 '14 at 03:28
  • @Edenia you have been write `size_t arrMod (BYTE ARR[3])`, `3` is fixed number. – BLUEPIXY Aug 15 '14 at 03:31
  • @Retired Ninja Thats just.. awfuly inappropriate. The duplicate you have found is slightly different then mine. Lets start with the fact it is related to `C++` and end up with the fact that it isn't the same question. – Edenia Aug 15 '14 at 03:31
  • @BLUEPIXY That because i have to pass something as a paddle. – Edenia Aug 15 '14 at 03:32
  • you can write `size_t arrMod (size_t N, BYTE ARR[N])...return N;`, call `arrMod(sizeof(arr), arr)` – BLUEPIXY Aug 15 '14 at 03:34
  • Returning the sizeof has to be done in the function. It is a function from a library i am writing and it needs to be brief for the user. – Edenia Aug 15 '14 at 03:37
  • 3
    Information of the size of an array will be lost is passed as a pointer to it when you pass an array to a function. Finding the size of the original array from the pointer is impossible. – BLUEPIXY Aug 15 '14 at 03:40
  • @Edenia Ah, for some reason I thought I saw a C++ tag, sorry. – Retired Ninja Aug 15 '14 at 03:54
  • Detail: `sprintf(stdout, "Size = %i\nARR[0] = %i\nARR[1] = %i\nARR[2] = %i", s, arr[0], arr[1], arr[2]);` strictly is undefined behavior as the first `"%i"` should be something like `"%zu"`. `sizeof(int)` might not equal `sizeof(size_t)`. It appears to be OK in this case. – chux - Reinstate Monica Aug 15 '14 at 04:02
  • and `sprintf` --> `fprintf` – BLUEPIXY Aug 15 '14 at 04:49

1 Answers1

2

The only way is to declare the parameter of the function as a pointer to an array. For example

size_t arrMod (BYTE ( *ARR )[3] )
{
    ( *ARR )[1] = 255;
    return sizeof( *ARR );
}

But in this case you can use only arrays with three elements.

Otherewise it is impossible to get the size of an array with an arbitrary number of elements in C.

According to the C Standard

3 Except when it is the operand of the sizeof 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. If the array object has register storage class, the behavior is undefined.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • if thats true.. thats my point. To restrict the user of declaring larger arrays. I will test it now. Are you able to make an explanation of how is that happaning in memory. – Edenia Aug 15 '14 at 03:38
  • I tested with declaring `arr[2]` it returns again 3. – Edenia Aug 15 '14 at 03:44
  • @Edenia As I wrote it is impossible in C to get the size of an array such a way because arrays are converted to pointers to their first elements. You have to include the size of an array as function parameter yourself. – Vlad from Moscow Aug 15 '14 at 03:55