Setting an array of strings

3 posts / 0 new
Last post
Last seen: 11 years 1 month ago
Joined: 05/06/2013
Posts: 2
Setting an array of strings

Given next field in a topic.

<member name="exerciseNickname"          type="string" arrayDimensions="4" stringMaxLength="20"/>

With the usual dynamicdata you have set some array types to set as double, byte, float, etc but there's no set_string_array.

Notice in my case i don't use idl file i use a file .qos to describe every topic instead.

How can i set the array of string?

Gerardo Pardo's picture
Last seen: 2 weeks 1 day ago
Joined: 06/02/2010
Posts: 602


You need to use the DynamicData::bind_complex_member operation to first bind your member. I will elaborate below.

A structure can contain members of different types (longs, strings, arrays, sequences, other structures). If a member is of a simple type, e.g. a primitive like a long, float, or even sequences of primitive types, then you can use the "set_xxx" operations directly on the structure to set the member.  However more complex members, like members that themselves have a structured type or arrays/sequences of non primitive types require the use of the  DynamicData::bind_complex_member() to bind another DynamicData object to the inner member. This can be done recursively until you get to a member of a type where you can use the  aforementioned "set_xxx" operations.

Assuming the data-type described by this XML:

    <struct name="EmbeddedStringArrayType">
        <member name="array_of_string_member" stringMaxLength="20" type="string" arrayDimensions="4"/>   

You can use the following C++ code to set the elements of array of strings inside:

        // This code assumes embeddedStringArray points to a DDS_DynamicData containing
        // a data of type EmbeddedStringArrayType
        DDS_DynamicData *embeddedStringArray = ... 
        // Define a DynamicData to hold a reference to the member inside embeddedStringArray
        DDS_DynamicDataProperty_t dd_property;
        DDS_DynamicData array_of_string(NULL, dd_property);

        /* Bind array_of_string to the member called "array_of_string_member" */
        retcode = embeddedStringArray->bind_complex_member(array_of_string, "array_of_string_member",
        if (retcode != DDS_RETCODE_OK) {
              printf("bind_complex_member (array_of_string) error %d\n", retcode);

        retcode = array_of_string.set_string(NULL, 1, "Hello 1");
        retcode = array_of_string.set_string(NULL, 2, "Hello 2");
        retcode = array_of_string.set_string(NULL, 3, "Hello 3");
        retcode = array_of_string.set_string(NULL, 4, "Hello 4");
        if (retcode != DDS_RETCODE_OK) {
            printf("set_string error %d\n", retcode);

I also uploaded an example that uses this API to the File Exchange it can be found at this link 


Last seen: 11 years 1 month ago
Joined: 05/06/2013
Posts: 2


That works fine!!