0

I have a C library with following struct

typedef struct Client_
{
    /*! Display Name for Guest only */
    char displayName[USERID_SIZE];
    /*! Conference PIN if needed */
    char pin[PIN_SIZE];
} Client;

From swift, I have to assign the members with string.

var client = Client();
let guestVName = "Swift user";
let guestVRoomPin = "";

How to do this? Please help.

1

1 Answer 1

1

You may need to write something like this:

strlcpy(&client.displayName.0, guestVName, Int(USERID_SIZE))
strlcpy(&client.pin.0, guestVRoomPin, Int(PIN_SIZE))

In C-language, arrays are passed by the pointer to the first element, and in Swift, fixed sized C-arrays are imported as tuples. So, you need to emulate what C-compiler does. In the code above, you can pass the pointer to the first element of the tuple.

(Remember, strlcpy is not a Standard C-library function. And it cuts off the exceeded part of the string, which may generate an invalid byte sequence as UTF-8.)

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.