returning struct variable from function - return

getting error : a.c:25:29: error: type of formal parameter 1 is incomplete
case 1 : data = insert( data,dev);
^~~~
a.c:25:19: error: incompatible types when assigning to type ‘struct Var’ from type ‘int’
case 1 : data = insert( data,dev);
[for function insert error : a.c:47:21: warning: ‘struct Var’ declared inside parameter list will not be visible outside of this definition or declaration
int insert( struct Var data, int sda[]){
^~~
a.c:47:25: error: parameter 1 (‘data’) has incomplete type
int insert( struct Var data, int sda[]){
]2

Related

File operation in drivers and struct declaration

i am trying to figure out how the code behind a basic kernel driver works.
I have the following struct:
static struct file_operations fops =
{
.open = dev_open,
.read = dev_read,
.write = dev_write,
.release = dev_release,
};
And my dev_open function is defined as:
static int dev_open(struct inode *, struct file *);
Now Im also familiar with the fact that the prototype for opening a device file is defined in the linux/fs.h:
http://lxr.linux.no/linux+v3.10/include/linux/fs.h#L1517
Here is the specific line from that link:
int (*open) (struct inode *, struct file *);
Now my question is what is the relationship between .open = dev_open, and int (*open) (struct inode *, struct file *);
which is defined in linux/fs.h? Is it passing the address of dev_open to the function pointer int (*open) defined in the linux/fs.h? There must be some relation or what is the point of defining the struct fops as type "file operation"?
A similar question was asked and answered here but i feel that my question was left out:
File operations in drivers
Thank you
I think this question is more about C than the Linux kernel.
Members of structure or union types cannot have function type, but they can have pointer to function type. For example, in the Linux kernel, the open member of struct file_operations needs to be declared with a pointer to function type: int (*open)(struct inode *, struct file *);. Declaring the member as int open(struct inode *, struct file *); is an error.
In this variable definition in Linux kernel code:
static struct file_operations fops =
{
.open = dev_open,
.read = dev_read,
.write = dev_write,
.release = dev_release,
};
Incidentally, the above should normally have the owner member initialized like so:
.owner = THIS_MODULE,
The expressions dev_open, dev_read, dev_write and dev_release are function designators being used as assignment expressions to initialize the members of fops. A function designator is an expression that has function type. Unless it is the operand of sizeof, _Alignof, or the unary & operator, a function designator is converted to a pointer to function type. Therefore, the above definition of variable foo is exactly equivalent to:
static struct file_operations fops =
{
.open = &dev_open,
.read = &dev_read,
.write = &dev_write,
.release = &dev_release,
};
(Don't forget to also initialize .owner = THIS_MODULE,.)
There, the function designators are operands of the unary & operator and so are not converted to pointer to function types implicitly, but the & operator is converting them to pointer to function types explicitly.
After the above initialization of fops, rc = fops.open(inode, file); indirectly calls dev_open(inode, file) and assigns the return value to rc. You may sometimes see this written in an older style: rc = (*fops.open)(inode, file);. They both do the same thing. The operand of the function call operator ( ) is in fact always a pointer to a function. In the case of rc = (*fops.open)(inode, file);, fops.open has a pointer to a function type. (*fops.open) dereferences fops.open to a function type but since (*fops.open) is a function designator it is implicitly converted back to a pointer to function type before the function call. Similarly, in the direct call rc = dev_open(inode, file);, dev_open is a function designator and so has a function type, but is implicitly converted to a pointer to function type before the function call.

printing variable out of scope does not yield error

I had this weird problem in Dart. Consider the following code :
class Number {
int num = 10;
}
Here, I created a little class with a int object num
When I try to print it using the main() function OUTSIDE the class like :
main() {
print(num);
}
I get the output as :
num
Which is weird, since I expected an error. If I were to print a undefined variable as in print(foo); I would get an error, which is expected.
What I find even more interesting is the runtimeType of a variable whose value is num.
var temp = num;
print(temp.runtimeType);
}
The above code prints _Type, when I expected it to be int.
Can somebody please clear this?
The name num is a type declared in dart:core. It's the supertype of int and double.
When you do print(num); outside the scope where your int num; variable is declared, the num refers to that type from dart:core which is always imported and therefore in scope.
Dart type names can be used as expressions, they evaluate to a Type object.
So, you are printing a Type object for the type num, which prints "num", and the run-time type of that object, which is again a Type object, which prints _Type because that's the actual internal type of the Type object instance.

Cannot invoke initializer for type 'UnsafeMutablePointer<Int32>' with an argument list of type '(Int32?)'

I am trying to implement some methods and pointers of ffmpeg to Swift but converting it to Swift is a little complex. What does this error mean?
Cannot invoke initializer for type 'UnsafeMutablePointer' with an argument list of type '(Int32?)'?
Code is below
let pictureFrameData = av_malloc(Int(numBytes))
var test = frame?.pointee.linesize.0.
av_image_fill_arrays(UnsafeMutablePointer(frameRGB?.pointee.data.0),
UnsafeMutablePointer<Int32>(frame?.pointee.linesize.0)!,
pictureFrameData,
frameRGB?.pointee.format,
frameRGB?.pointee.width,
frameRGB?.pointee.height,
1)
the error is at this line UnsafeMutablePointer<Int32>(frame?.pointee.linesize.0)
If the function expects an 'UnsafeMutablePointer<Int32> argument
then you'll have to pass an Int32 value as “inout parameter” with &:
var linesize: Int32 = ...
av_image_fill_arrays(..., &lineSize, ...)

Cannot invoke initializer for type 'Int' with an argument list of type JSON

Why I cannot convert String value from the JSON response to Int? It returns me the next error:
Cannot invoke initializer for type 'Int' with an argument list of type '(JSON)'
When I try to:
Int(json[i]["id"])
How can I convert it to Int?
I assume you're using SwiftyJSON.
In that case you have two options:
The optional getter:
if let id = json[i]["id"].int {
// do something
}
The non optional getter:
let id: Int = json[i]["id"].intValue

Calling C function from Swift

I am trying to call a C function from Swift , but I do not know exactly how to define variables to pass parameters.
The function c is:
DBFGetFieldInfo( DBFHandle psDBF, int iField, char * pszFieldName, int * pnWidth, int * pnDecimals );
The main problem is pszFieldName, pnWidth and pnDecimals inout parameters. I tried made ​​:
var dbf:DBFHandle = DBFOpen(pszPath, "rb")
var fName:[CChar] = []
var fieldWidth:Int32 = 0
let fieldDecimals:Int32 = 0
let fieldInfo:DBFFieldType = DBFGetFieldInfo(dbf, i, fName, &fieldWidth, &fieldDecimals)
but it gives me an error
Cannot invoke 'DBFGetFieldInfo' with an argument list of type '(DBFHandle, Int32, [CChar], inout Int32, inout Int32)'
Expected an argument list of type '(DBFHandle, Int32, UnsafeMutablePointer<Int8>, UnsafeMutablePointer<Int32>, UnsafeMutablePointer<Int32>)'
Any ideas?
UnsafeMutablePointer<Int8>, UnsafeMutablePointer<Int32>, UnsafeMutablePointer<Int32>
You need to convert your variables to the appropriate types required by the method signature.
C Syntax:
const Type *
Type *
Swift Syntax:
UnsafePointer
UnsafeMutablePointer
This is covered by Apple in their Using Swift with Cocoa and Objective-C reference located here.
C Syntax -----> Swift Syntax
const Type * -----> UnsafePointer
Type * -----> UnsafeMutablePointer
The number of input and the types should be the same
To create an UnsafeMutablePointer<Int8> from a string use:
String(count: 10, repeatedValue: Character("\0")).withCString( { cString in
println()
// Call your function here with cString
})

Resources