How to translate data from c language void * pointer to [UInt8 ] in swift code.

A swift function name as getDataFromC which can connect with C function. A C function name as ExternalMethod which will receive the connect from swift. The parameter will be fill by call setData function.

I can confirm that the entire structure return to getDataFromC was successful. Because I print the data of other structure members to check the correctness of the data.

However, I don't know how to translate the largeData to [UInt8].

I try UnsafeMutableRawPointer type. I don't know what to do for the following action. I expected that output.largeData can be translate to [UInt8] type.

//Bridging header file
struct myStruct
{
    void *largeData;
	int largeDataLength;
	int smallData[3];
}
//C file
void setData(myStruct* data)
{
    int i = 0;
	uint8_t array[1024];
	
	memset(array,0x66,1024);
	
	data->largeData = array;
    data->largeDataLength = 1024;
	data->smalData[0] = 0x99;
	data->smalData[0] = 0x88;
	data->smalData[0] = 0x77;
}

void ExternalMethod(myStruct* data)
{
    setData(&output);
}
//Swift file
func test() {

    
    var output = myStruct()
	getDataFromC(&output);
	var myPointer: UnsafeMutableRawPointer = output.largeData
	/* Here: I don't know how to translate data from myPointer to [UInt8] */
	
	print("smallData: \(output.largeDataLength)") // 1024 transmission is correct
	print("smallData: \(output.smallData.0)")     // 0x99 transmission is correct
	print("smallData: \(output.smallData.1)")     // 0x88 transmission is correct
	print("smallData: \(output.smallData.2)")     // 0x77 transmission is correct
}

Anything that's not declared within the struct is deallocated from the stack when set data returns.

typedef struct {
    uint8_t *memo;
    uint8_t array[5];
    int why[4];
    char *books;
} Bounce;

implemenation of fix.

void fix(Bounce *model) {
    memset(model->array,0x53,5);
    model->why[0] = 2;
    model->why[1] = 3;
    model->why[2] = 2;
    model->why[3] = 2;
    model->books = "Hello world";
    model->memo = model->array;
}
import Foundation

var bounce = Bounce()
fix(&bounce)

bounce.books.map({print(String(cString: $0))})

for value in 0..<5 {
    print(bounce.memo.advanced(by: value).pointee)
}

let why = bounce.why
print(why)

Output:

Hello world
83
83
83
83
83
(2, 3, 2, 2)
Program ended with exit code: 0

With the void* or anonymous type.

typedef struct {
    void *memo;
    uint8_t array[5];
    int why[4];
    char *books;
} Bounce;
for value in 0..<5 {
    let thing = bounce.memo.assumingMemoryBound(to: UInt8.self).advanced(by: value).pointee
    print(thing)
}

Hi MobileTen,

Thank you for your reply. I follow your method to add the following code in swift:

for value in 0..<5 {
    let thing = output.largeData.assumingMemoryBound(to: UInt8.self).advanced(by: value).pointee
    print(thing)
}

However, the output value of thing not the correctly.

Is anything I've missed or wrong setting?

How to translate data from c language void * pointer to [UInt8 ] in swift code.
 
 
Q