Passing arrays to functions & pointers

Hi all,

Consider the following function:

void LogPacket (byte *packet, char *description,byte Description_len,byte location) 

{....

}

The function above logs on SD card the packet received, with description text of length Description_len. The location is just a flag telling the function where to put the data.

The above could also be defined like this :

void LogPacket (byte packet[70], char description[20], byte Description_len,byte location) 

{....

}

Both definitions compile just fine and do the same job.

My question is how does the compiler know in the first case what size of an array to expect? Can someone with more experience explain the implications of both and which one is best to use ?

My question is how does the compiler know in the first case what size of an array to expect?

It does not in either version, you have to pass the length of the string.

If it is zero terminated string, the (used) length can be computed by strlen().

It does not in either version, you have to pass the length of the string.

I would have thought that in the second case the max size is defined in brackets. : packet[70]

So it can expect an array of max size 70.

Edit: Reason I am looking into this is because I am trying to troubleshoot some possible memory corruption issues.

If the compiler sees this:

void LogPacket (byte packet[70], char description[20], byte Description_len,byte location)

as either the signature for the function or its function prototype, it is used strictly for constructing an attribute list for the function. The parameters are only used for type checking. Both arrays are resolved to a pointer with an lvalue that is the starting address for the array. C seems to always assume that you know what you're doing, so it does not do boundary checking.

You could also change the signature to:

void LogPacket (byte packet[], char description[], byte Description_len, byte location)

and the compiler will be just as happy because each array still resolves to a pointer.

Nope, arrays are internally pointers as far as the actual binary program goes. Brackets are just a nice to have notation.

The function "receives" a pointer to a byte in both cases. How it's implemented depends on the language and compiler, don't remember how it's done in C, but it's just a pointer for sure.

Remember that you don't actually copy the array for the function. So the compiler doesn't need to know the length of it, just where it starts and how many bytes each cell occupies (so it can "jump" to the an arbitrary cell index).

Some compilers, not sure what happens with the Arduino, can detect and warn you about an out of boundary access to an array and can even insert code into your binary to check for that when the program is running. I don't think we have that in C?

Edit: I know ecojack posted something similar. But I had already typed mine so... :D

Watcher: My question is how does the compiler know in the first case what size of an array to expect? Can someone with more experience explain the implications of both and which one is best to use ?

I think you've gotten enough good explanations for your first question.

As for your second question, use the first form. The second one tends to confuse new programmers into thinking that the function knows the size of the array (like it confused you).

This gets more complicated with multidimensional arrays BTW. You're not there yet, but be prepared for it.

Thanks a lot for your answers. Its all clear now... (still have to figure out the source of mem corruption!)