This question already has answers here:
What does dot (.) mean in a struct initializer?
(4 answers)
Closed 9 years ago.
From fuse/examples/fsel.c
static struct fuse_operations fsel_oper = {
.getattr = fsel_getattr,
.readdir = fsel_readdir,
.open = fsel_open,
.release = fsel_release,
.read = fsel_read,
.poll = fsel_poll,
};
this is the definitionof fuse_operations
struct fuse_operations_compat25 {
int (*getattr) (const char *, struct stat *);
int (*readlink) (const char *, char *, size_t);
int (*getdir) (const char *, fuse_dirh_t, fuse_dirfil_t);
int (*mknod) (const char *, mode_t, dev_t);
int (*mkdir) (const char *, mode_t);
int (*unlink) (const char *);
int (*rmdir) (const char *);
.....
};
so what do those . mean? It's the first time I see that
It means that the field that is named after the . will have that value.
For example, the gettr function pointer will point to the fsel_getattr function.
Related
I am trying to make a program that can pass a custom datatype like struct for example to another code using exec function.
So I was wondering if there is any possible way for me to do it.
IK that exec has its own syntax to accept file name and a list of string (char*) parameters but I want to do some illegal coding.
Sample Code:
struct Student {
int std_roll_len;
char * student_roll;
int std_name_len;
char * student_name;
float student_cgpa;
void (*std_data_input) (struct Student*);
void (*std_data_display)(struct Student*);
void (*std_data_destroy)(struct Student*);
};
extern void std_data_input (struct Student*);
extern void std_data_display(struct Student*);
extern void std_data_destroy(struct Student*);
int main() {
char filename[] = "./Task_1_Receiver";
char std_cgpa[16];
struct Student student = {0, NULL,
0, NULL, 0.0,
std_data_input,
std_data_display,
std_data_destroy};
student.std_data_input(&student);
sprintf(std_cgpa, "%g", student.student_cgpa);
//char std_id [] = "F2019266178";
//char std_name[] = "Mehroz Mustafa";
//char std_cgpa[] = "1.0";
//-> char* argv[] = {std_id, std_name, std_cgpa};
printf("Opening Student_Receiver!\n");
execl(filename,
student.student_roll,
student.student_name,
std_cgpa, NULL);
printf("Closing Student_Receiver.exe!\n");
// any on of exec func (filename, struct Student, NULL); THIS ILLEGAL STATEMENT TO BREAK THE LAWS OF CODING
return 0;
}
This question already has answers here:
How can I call a function using a function pointer?
(16 answers)
Closed 1 year ago.
I have a struct with a function pointer, that is intended for pointing to the bar() function, but i have no clue how to call the function that's being pointed on:
#include <stdlib.h>
typedef struct Foo
{
int (*func_ptr)(const char *, const char *);
} Foo;
int bar(const char *a, const char *b)
{
return 3;
}
int main(void)
{
Foo *foo = (Foo *)malloc(sizeof(Foo));
foo->func_ptr = &bar;
//how do i call the function?
return 0;
}
Imagine your const char *a is "hello" and const char *b is "there", then you can use any of the following forms to call the function by its pointer:
(foo->func_ptr)("hello", "there");
(*foo->func_ptr)("hello", "there");
foo->func_ptr("hello", "there");
This question already has answers here:
How can I get/set a struct member by offset
(6 answers)
Closed 2 years ago.
I have struct in c
struct Book {
char title[50];
char author[50];
char subject[100];
int book_id;
};
struct Book * book;
I can access the integer book_id like book->book_id
But how can I access to book_id by offset? How can I calc (in c code) the offset of specific element in struct and access like book+X
#define offset(type, member) ((size_t)&(((type *)0) -> member))
#define ACCESS(object, type, offset) (type *)(((char *)&(object)) + (offset))
typedef struct
{
int a,b,c;
}t;
int main(void)
{
t s = {1,2,3};
printf("%zu\n", offset(t,b));
printf("%d\n", *ACCESS(s, int, offset(t,b)));
}
I was unable to understand what do these 2 round parenthesis mean ?
struct conf_method_st {
const char *name;
CONF *(*create) (CONF_METHOD *meth);
int (*init) (CONF *conf);
int (*destroy) (CONF *conf);
int (*destroy_data) (CONF *conf);
int (*load_bio) (CONF *conf, BIO *bp, long *eline);
int (*dump) (const CONF *conf, BIO *bp);
int (*is_number) (const CONF *conf, char c);
int (*to_int) (const CONF *conf, char c);
int (*load) (CONF *conf, const char *name, long *eline);
};
I am guessing some sort of type-cast or initialization is going on. Can anyone explain ?
This is the syntax for a function pointer type.
Taking the first example:
CONF *(*create) (CONF_METHOD *meth);
It defines a struct member named create which points to a function receiving a single parameter of type CONF_METHOD* and returning a value of type CONF*
So, for example, if you had a function:
CONF *my_create(CONF_METHOD* meth)
{
//...
}
Then you could store a pointer to that function in your struct:
struct conf_method_st c;
c.create = my_create;
And you can invoke it just like a function:
CONF *conf = c.create(meth);
What I have to put as second parameter in this function? I need to understand the meaning of int (*fn)(const char *, const struct stat *ptr, int flag).
int
ftw(const char *path, int (*fn)(const char *, const struct stat *ptr, int flag),
int depth);
Thank you!!
int (*fn)(const char *, const struct stat *ptr, int flag)
is a pointer to a function that returns an int and takes a const char*, a const struct stat *, and an int.
If you had this function:
int func (const char *s, const struct stat *ptr, int flag)
{
return 0;
}
You could pass func as that argument.