I'm greeting all.
My question on C. Is it possible to cast the function's argument type in its interface directly?
<linux/serial_core.h>
contains
struct uart_port {
many elements
}
In kernel v5.4 one of them is unsigned char unused[2];
but later it was droped. Linux_doc advices create own structure and to register
it, enter kernel's struct from begin and what is needed else add after, for example
typedef uart_port_15 {
struct uart_port port;
unsigned char unused[32];
} uart_port_15;
The device driver, which I trying to correct, contains the great number functions,
expecting struct uart_port *P
. In text it is no problem to cast the pointer to structure
to pointer on its 1st element or to back, but it is possible make it in the function title?
Simple example
int func(struct uart_port *p) {
something
}
Can I cast struct uart_port_15 *p1
or &(struct uart_port_15 *p1->port)
to struct uart_port *p
directly in func's calling?
I tried different variants, but without success.
Thanks for the answers. For example
#include <linux/serial.h>
#include <linux/serial_core.h>
typedef uart_port_15 {
struct uart_port port;
unsigned char unused[32];
} uart_port_15;
int func1((struct uart_port *)(struct uart_port_15 *p)){
}
int func2((struct uart_port *)(struct uart_port_15 *p1) p){
}
int func3((struct uart_port *)&(struct uart_port_15 *p1->port)){
}
int func4((struct uart_port *)(struct uart_port_15 *p1) p){
}
int func5((struct uart_port *p)&(struct uart_port_15 *p1->port)){
}
Neither of these not works and others too. gcc writes that "declaration specifier need" before (struct uart_port *). Perhaps I'm not understand C syntax or would like the impossible.
The next variant was compiled:
#include <linux/serial.h>
#include <linux/serial_core.h>
typedef uart_port_15 {
struct uart_port port;
unsigned char unused[32];
} uart_port_15;
struct uart_port_15 *p1;
struct uart_port * p_152p(struct uart_port_15 *);
struct uart_port * p_152p(struct uart_port_15 *p){
return (struct uart_port *)p;
}
int func(struct uart_port * p_152p(struct uart_port_15 *p1)){
}
But it is not what I need. I need that this function (func) has interface "int func(struct uart_port *)" because the pointer to it must be assign to pointer in struct, the its element waits such type, but func's argument type is changed at the such calling.
In my case I can't use the advice from answer. I must to give the function by predefined type, (struct uart_port *p).
CodePudding user response:
Is it possible to cast the function's argument type in its interface directly?
No. The “interface” you are talking about is a declaration that tells the compiler about the function to be called. It does not define operations to be performed when the function is called, except for automatic conversions of argument types to parameter types. Those automatic conversions will not convert between pointers to different structure types.
Often, converting between pointers to different structure types when calling a function is a sign of some mistake. However, if you wish to do it, you could simply define an auxiliary function to do the conversions. For example:
// Declaration of original function to be called.
int foo(struct FooStructure *p);
// Definition of alternate interface to function.
static int bar(struct BarStructure *p)
{
// Convert pointer and call original function.
foo((struct FooStructure *) p);
}
You could go further and define a macro to replace uses of the original function with the alternate one:
#define bar foo
As noted above, this may be a bad idea and should be avoided.