pass Int to C function

user842225

I am writing swift code & need to invoke a C function.

var idx = 5
//ERROR: Cannot invoke c_func with argument list of type '(Int)'
c_func(idx)

The C function signature is:

void c_func(int idx)

So, my question is how can I pass swift Int to C function which accepts int as parameter?

Martin R

The C type int is mapped to Swift as

/// The C 'int' type.
typealias CInt = Int32

Therefore you have to declare the variable as CInt:

var idx : CInt = 5
c_func(idx)

or convert the Int to CInt:

var idx = 5
c_func(CInt(idx))

(Of course you can use Int32 instead if you prefer to emphasize that it is a fixed-size integer type.)

More information about the mappings between (Objective-)C types and Swift types can be found in the "Using Swift with Cocoa and Objective-C" documentation.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I pass a struct and an int from an function and back to main? C

From Dev

Pass str as an int array to a Python C extended function (extended using SWIG)

From Dev

C pass pointer as argument in function

From Dev

Pass C function callback in Swift

From Dev

C# pass function as argument

From Dev

Pass a C array to a Rust function

From Dev

C# pass function as argument

From Dev

how to pass pointers to a function in C

From Dev

Pass char pointer to function in C

From Dev

C: Pass 2D int array

From Dev

C Ways To Pass Function As Argument To Function

From Dev

Trying to pass int* to function and to function inside another function.int* was declared in main

From Dev

C assign an int to a function pointer

From Dev

c++ overload 3 function with different parameters(int,*int,&int)

From Dev

Pass List<int> from code behind to use in Javascript function

From Dev

Pass List<int> from code behind to use in Javascript function

From Dev

C# : Pass int array to c++ dll

From Dev

Pass int by reference from C++/CLI to C#

From Dev

how to pass void * (pointing to double or int) from C to C#

From Dev

How to pass an array of char's to a function in C

From Dev

C++ Pass FILE * as argument to a function

From Dev

c++ pass array directy to function

From Java

How do you pass a function as a parameter in C?

From Dev

C++11 constexpr function pass parameter

From Dev

C++ Pass a member function to another class

From Dev

Pass a function template as a parameter in C++

From Dev

C++ pass by value / by reference function overload

From Dev

Pass a function-pointer name to C Preprocessor

From Dev

C string function calls, pass by value or reference?

Related Related

HotTag

Archive