0

I am porting an obscure library form C to dart.

a property is defined on a struct like this unsigned char payload[256];

I interpret that as an array of chars. So I converted it to List<int> payload;

later in the original library there is this code

parser->handleDataValue( extendedCodeLevel, code, numBytes, parser->payload+i, parser->customData );

where 'i' is an index for a loop

I translated that to

parser.handleDataValue(extendedCodeLevel, code, numBytes, parser.payload + i, parser.customData);`

Now I am dealing with the error The argument type 'int' can't be assigned to the parameter type 'List<int>'.

I understand the dart side of the problem but I don't understand what the original C means to write its dart equivalent.

7
  • 2
    It performs pointer arithmetic. The other way to write it in C would be &(parser->payload[i]) Commented Dec 2, 2021 at 10:38
  • For any array or pointer p and index i, the expression p[i] is exactly equal to *(p + i). From this can be deduced that p + i is a pointer to the element at index i (i.e. &p[i]). Commented Dec 2, 2021 at 10:38
  • @Someprogrammerdude ah, so does that mean that it is pointing to the next element in the payload array? Commented Dec 2, 2021 at 10:40
  • 2
    @xerotolerant: No. Adding 1 points to the next element. Adding i points to the element displaced i elements from the starting point. Commented Dec 2, 2021 at 10:45
  • Ohh, I get it now thank you. Commented Dec 2, 2021 at 11:32

1 Answer 1

1

I'd probably translate unsigned char payload[256]; in C to Uint8List payload; in Dart. That's an actual list of unsigned "char"s.

The pointer arithmetic can then be emulated by Uint8List.sublistView(parser.payload, i), which creates a view of the data of payload starting from the ith value, without copying, just like the C code.

Q.v. Uint8List.sublistView.

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

1 Comment

Than you. that's exactly what the code was doing. In ended up with a much hacker solution but I'll change to this as It makes the most. sense

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.