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)));
}
Related
This question already has answers here:
Structure padding and packing
(11 answers)
Why isn't sizeof for a struct equal to the sum of sizeof of each member?
(13 answers)
Closed 3 months ago.
This post was edited and submitted for review 3 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Why is the size of these two struct different depending on #include <stdbool.h> vs. typedef enum { false, true } bool; ?
#include <stdio.h>
#include <stdbool.h>
//typedef enum { false, true } bool;
struct x {
bool a;
int b;
char c;
};
struct y {
bool b;
char a;
int c;
};
int main(void) {
struct x x;
struct y y;
printf("Size of struct x:\t %zu\n", sizeof(x));
printf("Size of struct y:\t %zu", sizeof(y));
return 0;
}
With #include <stdio.h> the results on my machine (Mac, Intel):
Size of struct x: 12
Size of struct y: 8
I assume the size difference (i.e. 8 and 12) here is due to C utilizing some kind of memory structure padding.
With typedef enum { false, true } bool; the result:
Size of struct x: 12
Size of struct y: 12
Note: The question is not about structured padding (read more about that here and here), but why #include <stdbool.h> vs.
typedef enum { false, true } bool; gives different results to the same lines of code.
This question already has answers here:
How to find the size of an array (from a pointer pointing to the first element array)?
(17 answers)
Closed 5 years ago.
I have the following minimal example and I don't get, why my struct sizes are wrong. I'm expecting the output to be 50, instead I get 1. What am I doing wrong?
#include <stdio.h>
#include <stdlib.h>
typedef struct prod {
char *x;
} prod_t;
typedef struct obj {
prod_t *things;
} obj_t;
#define LARGE_BUF 100
#define CHAR_BUF 20
obj_t *func1(obj_t *t) {
t->things = malloc(sizeof(prod_t) * LARGE_BUF);
for (uint16_t i = 0; i < 50; i++) {
t->things[i].x = malloc(sizeof(char) * CHAR_BUF);
t->things[i].x = "hello";
}
return t;
}
int main(int argc, char **argv) {
obj_t *var = malloc(sizeof(obj_t));
var = func1(var);
printf("%lu\n", sizeof(var->things)/sizeof(var->things[0]));
return 0;
}
Since I don't have the number of entries, the function generated for me (it's 50 now, but it could change dynamically), how do I free(..) this up?
Is the only option to introduce a field in the struct, to keep track of the actual array size?
Yes you will need to add another member to the struct. For example a string wrapper type keeps track of the number of characters in it:
typdef struct {
char *base;
size_t n;
} string;
Notice n is of size_t, not int.
This question already has answers here:
Access Item in Nested Structure in C
(2 answers)
Closed 6 years ago.
typedef struct _imat {
int **m_mat;
int rows, cols;
} intMat;
typedef struct _banker {
intMat A;
intMat M;
int *C;
int numRes;
int numProcs;
} banker;
int main(int argc, char* argv[])
{
banker *b,c;
b = &c;
matInit((*b).A,(*b).numProcs,(*b).numRes);
}
I am trying to access intMat A in _banker struct but getting error:
"expected ‘struct intMat *’ but argument is of type ‘intMat’ void matInit(intMat *mat,int rows, int cols){"
(*b).A is of type intMat
However matInit is expecting a intMat *
So replace (*b).A with &(*b).A, for the "&" will make it a pointer
This question already has answers here:
Understanding typedefs for function pointers in C
(8 answers)
Closed 7 years ago.
typedef void (*Hello)(struct test1 *, test2 *, int a, int b, const int c *, int d);
In this case, I am confused by how to handle the struct as the argument.
I have written:
Hello p1;
(*p1)(....need some arguments to be added here);
Please kindly teach me how to complete this maybe sample code could help.
Thanks
Here is some code
struct point {
int x;
int y;
};
typedef void (*Hello)(struct point *p);
void resetPoint(struct point *p)
{
p->x = 10;
p->y = 0;
}
int main(void)
{
struct point dot;
Hello p1 = resetPoint;
p1(&dot);
printf("%d\n",dot.x);
return 0;
}
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.