0

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:

  1. data + 1 -> data[1]
  2. data + 1 -> data[0] + 1
  3. 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

8
  • *(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). Commented Nov 18, 2015 at 15:49
  • Data is a pointer. By doing `data + 1', you move your pointer to the next element in memory. Commented Nov 18, 2015 at 15:50
  • data is not an array, but a pointer. Possibly to the first element of a data_t array. In C you cannot pass an array as argument, only pointers. Commented Nov 18, 2015 at 15:54
  • I'd start by rewriting the macro to a function: 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 declaration data_t* const suggests 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. Commented Nov 18, 2015 at 15:57
  • 1
    the macro is in fact invalid (need to use \ for multi-line macro), and its use as shown will cause an error (data + 1[0] is not valid C) Commented Nov 18, 2015 at 16:00

2 Answers 2

2

In C pointers and arrays are usually interchangeable.

data_t* is equivalent to data_t[] in most cases, and the use of the array notation is used to simplify the dereferencing of the pointers for assignment.

data[0] = 1;
data[1] = 2; 

can be replaced with

*(data) = 1;
*(data+1) = 2;

It really is just semantics, although the use of the macro is ugly. Either way you can access out of range memory locations and cause trouble when accessing memory which is unassigned.

Update:

*(data+1) != data + 1

data + 1 - Memory location at the pointer data + 1 * (size of data type of the pointer). In other words, it is one unit over from the position of data.

*(data + 1) - Value at memory location of said data type.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for this input. But just because it's C, i want to state it more precisely. Is *(data + 1) = data + 1?
It's always interesting to see an accepted answer with 0 upvotes... how can an answer solve the OP's problem but at the same time not be useful?
@Michael I updated the answer. Basically *(data+1) does not equal data + 1
2

In this context:

data + 1

Means:

data + sizeof(data_t);

Because data is a pointer of type data_t pointer arithmetic is applied so that + 1 results in sizeof(data_t) bytes further than data.

So your first assumption is correct.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.