C return type in Swift


  • In my code I didn't have struct definition in the header file.
  • My bad when I created this post, I had pasted the struct definition in the header.


  • I have some C code that returns a struct Car
  • I am able to invoke the code from Swift
  • The return type in Swift is an Opaque Pointer


  • How can I read data from the opaque pointer ?
  • Is there a way to convert it to a Swift type ?
  • Or is there a better way of handling this scenario ?


#ifndef Car_h
#define Car_h

struct Car* f1(void);

#endif /* Car_h */


#include "Car.h"
#include <stdio.h>
#include <stdlib.h>

struct Car {
    int *prices;
    int doors;

struct Car* f1() {
    struct Car *car = malloc(sizeof(struct Car));
    car->prices = malloc(sizeof(int) * 2);
    car->prices[0] = 10;
    car->prices[1] = 20;
    car->doors = 4;
    return car;


import Foundation

let result = f1()

print("result = \(result)")
1 Like

Sorry was my fault, when I posted I pasted the struct definition in the header, in my real code the struct definition was in the .c file.

Cause of the problem:

  • Struct definition was not in the header, moving it to the header solved the problem.