I have a seemingly simple question regarding array pointer in C. I am trying to understand a portion of code written in C, so i can port it to C#. The data types and functions are defined as follows:
/* header file */
/* definition of data_t type */
typedef unsigned short uint16_t;
typedef uint16_t data_t;
/* the function the type data_t is used */
#define FOO(d) do {
d[0] = 1;
d[1] = 2;
} while (0)
/* source file */
/* the function where FOO is used */
static int BAR(data_t* const data)
{
FOO(data + 1);
}
When calling FOO(..) within BAR(..), what does "data + 1" mean? As i understand, data is an array from type data_t. I wasn't able to find an exact example on stackoverflow or else, therefore i am confused about the meaning of it. I have three options in my mind how an appropriate assignment in C# could look like:
- data + 1 -> data[1]
- data + 1 -> data[0] + 1
- data + 1 -> new data_t[] (new instance of data_t array at address + 1)
The first option makes sense to me. But when taking a look into the function FOO(..), it makes no sense, because FOO is using "data" like an array.
Can anyone give me a hint?
Thanks,
Michael
*(data+1)==data[1].(data+1)[1]==data[2]. You can use any pointer as array (there is no guarantee this is a valid memory address, however).datais not an array, but a pointer. Possibly to the first element of adata_tarray. In C you cannot pass an array as argument, only pointers.inline void (data_t* data) { d[0]=1; d[1]=2 }or just drop it entirely. The presence of that icky macro plus the pointless declarationdata_t* constsuggests that the person who wrote the code were fond of "lets make the code as complex as possible by using various obscure tricks" but in reality doesn't quite know what they are doing.\for multi-line macro), and its use as shown will cause an error (data + 1[0]is not valid C)