How do I assign value when structure has a void *pointer - c

I am trying to assign a value to variable which has a void *pointer. Below is the assignment as best as I understand. (Doesn't work)
void set_fb_video ()
{
goldfish_lcd.dev.(atmel_lcdfb_info*)platform_data->default_monspecs->modedb->xres = 10;
};
Below are part of structures as defined in header files
Header files
struct fb_videomode {
..
u32 xres;
..
};
struct fb_monspecs {
..
struct fb_videomode *modedb;
..
};
struct atmel_lcdfb_info;
struct atmel_lcdfb_info {
..
struct fb_monspecs *default_monspecs;
..
};
struct device {
..
void *platform_data;
..
};
struct platform_device {
..
struct device dev;
..
};
C files
struct fb_videomode at91_tft_vga_modes[] = {
..
.xres = 100,
..
};
struct fb_monspecs &at91fb_default_monspecs = {
..
.modedb = at91_tft_vga_modes,
..
};
struct atmel_lcdfb_info ek_lcdc_data = {
..
.default_monspecs = &at91fb_default_monspecs,
..
};
struct platform_device goldfish_lcd ={
..
.dev = {
..
.platform_data = &ek_lcdc_data,
},
..
};
Any suggestions are greatly appreciated.
Thank you in advance

Your cast isn't in the correct place, and you don't have a type called atmel_lcdfb_info:
((struct atmel_lcdfb_info*)goldfish_lcd.dev.platform_data)->default_monspecs->modedb->xres = 10;

Related

Warning from gcc: braces around scalar initializer, how to fix?

At rewriting kernel driver I got this warning:
msm-cirrus-playback.c:545:2: warning: braces around scalar initializer
Read that this warning appears when I am declaring one structure's field in {}:
struct random_struct test = {
{ .name = "StackOverflow" },
{ .name = "StackExchange" },
};
But my structure have 2-3 fields in {}:
static struct device_attribute *opalum_dev_attr = {
{
.attr->name = "temp-acc",
.show = opsl_temp_acc_show,
.store = opsl_temp_acc_store,
},
{
.attr->name = "count",
.show = opsl_count_show,
.store = opsl_count_store,
},
{
.attr->name = "ambient",
.show = opsl_ambient_show,
.store = opsl_ambient_store,
},
{
.attr->name = "f0",
.show = opsl_f0_show,
.store = opsl_f0_store,
},
{
.attr->name = "pass",
.show = opsl_pass_show,
},
{
.attr->name = "start",
.show = opsl_cali_start,
},
};
This structure:
struct device_attribute {
struct attribute attr;
ssize_t (*show)(struct device *dev, struct device_attribute *attr,
char *buf);
ssize_t (*store)(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count);
};
How can I fix this warning? Qualcomm kernels are building with -Werror flag, so this warning is critical.
static struct device_attribute *opalum_dev_attr means declare opalum_dev_attr as static pointer to struct device_attribute
Your code is trying to initialize a static array of struct device_attribute
What you want is: static struct device_attribute opalum_dev_attr[] which means declare opalum_dev_attr as static array of struct device_attribute
It is because you initialize the pointer to the struct not the struct itself.
you need to assign the reference to the struct for example by using compound literals
struct x
{
int a,b,c;
}a = {1,2,3};
void foo()
{
struct x a = {1,2,3};
struct x *b = {1,2,3}; // wrong warning here
struct x *x = &(struct x){1,2,3}; // correct reference to the struct assigned (using compound literal
struct x *y = (struct x[]){{1,2,3}, {4,5,6}, {4,5,6}, };
struct x z[] = {{1,2,3}, {4,5,6}, {4,5,6}, };
}

Pointers to functions casting

I have the following structs:
struct mtmFlix_t {
List usersList;
List seriesList;
};
struct User_t {
int age;
char* username;
MtmFlix mtmFlix;
};
These are the typedefs in list.h :
typedef void* ListElement;
typedef ListElement(*CopyListElement)(ListElement);
typedef void(*FreeListElement)(ListElement);
These are the typedefs in user.h and MtmFlix.h :
typedef struct User_t *User;
typedef struct mtmFlix_t* MtmFlix;
I would like to use the following function in mtmflixCreate, but I can't seem to figure out how to cast the UserCreate and UserFree to (*void) ?
List listCreate(CopyListElement copyElement, FreeListElement freeElement);
MtmFlix mtmFlixCreate()
{
MtmFlix newMtmFlix = malloc(sizeof(*newMtmFlix));
if (newMtmFlix == NULL) {
return NULL;
}
newMtmFlix->seriesList=listCreate(?????);
newMtmFlix->usersList=listCreate(?????);
}
The following functions appear in user.h :
User UserCreate(MtmFlix mtmFlix, const char* username,int age);
Void UserFree(User user);
You don't. You have to create functions that have the needed types. Something like this:
ListElement CopyUserListElement(ListElement elem) {
// (ListElement) is not necessary here, but included for completeness
return (ListElement)CopyUser((User_t*)elem);
}
void FreeUserListElement(ListElement elem) {
UserFree((User_t*)elem);
}

gcc complaints about braces in initialiser (nested structs)

In chasing down a very opaque bug, I have been trying to compile with no warnings or errors. This part of my code worked fine, but gcc complains about the braces--it says there are braces missing and extra braces. I usually initialise a bit more sloppily but here I'm being as pedantic as possible with braces for each logical level of inclusion. The only struct I really care about initialising is the last one, the Ccfg. I thought I'd build up to it gradually as it contains nested other structs, but apparently even the ones preceding it are mis-initialized according to gcc.
Here's the code:
#define max_nodes 24
struct Cseg
{
int begin;
int arc;
int end;
};
struct Node
{
struct Cseg exit[4];
};
struct Core
{
int num_circles;
int num_nodes;
struct Node node[max_nodes];
};
struct Ccfg
{
struct Core core;
int dummy1;
int dummy2;
};
int main(void)
{
struct Cseg A = {0,1,2};
struct Node B =
{
{0,1,2}, {1,3,0}, {2,-1,3}, {0,-2,1}
};
struct Node C =
{
{0,1,2}, {1,3,0}
};
struct Core D =
{4, 4,
{
{ {0,1,2}, {1,3,0}, {2,-1,3}, {0,-2,1} },
{ {1,3,0}, {2,1,0}, {3,-2,1}, {2,-1,0} },
{ {3,1,2}, {0,1,2}, {1,-3,0}, {2,-3,1} }
}
};
struct Ccfg E =
{
{2, 2,
{
{ {0,1,1}, {0,2,1} },
{ {1,2,0}, {1,1,0} }
}
}
};
return 0;
}
Some of the initialisations are incomplete; that is deliberate. My real ccfg struct has many more fields but I've simplified it for this post. If someone could let me know what I'm doing wrong I'd appreciate it a lot. Thanks!
EDIT: in my working code, the initialiser for struct Ccfg E omits the innermost braces, and works fine (but gcc still warns me about it). I added them into this test because they seemed logically appropriate, but they actually generate an error--which I don't understand.
You are missing braces in some places. Specifically, if you have an array of structs, the entire array needs to be brace-wrapped; you were just wrapping each struct entry. I just added braces as needed and it works fine now. http://ideone.com/fork/HqxB9R
#define max_nodes 24
struct Cseg
{
int begin;
int arc;
int end;
};
struct Node
{
struct Cseg ex[4];
};
struct Core
{
int num_circles;
int num_nodes;
struct Node node[max_nodes];
};
struct Ccfg
{
struct Core core;
int dummy1;
int dummy2;
};
int main(void)
{
struct Cseg A = {0,1,2};
struct Node B =
{
{ {0,1,2}, {1,3,0}, {2,-1,3}, {0,-2,1} }
};
struct Node C =
{
{ {0,1,2}, {1,3,0} }
};
struct Core D =
{4, 4,
{
{ { {0,1,2}, {1,3,0}, {2,-1,3}, {0,-2,1} } },
{ { {1,3,0}, {2,1,0}, {3,-2,1}, {2,-1,0} } },
{ { {3,1,2}, {0,1,2}, {1,-3,0}, {2,-3,1} } }
}
};
struct Ccfg E =
{
{2, 2,
{
{ { {0,1,1}, {0,2,1} } },
{ { {1,2,0}, {1,1,0} } }
}
}
};
return 0;
}

Proper way to initialize a const member of dynamically allocated struct

I have these 2 structs:
struct Params {
int a;
int b;
};
struct Foo {
const struct Params settings;
int state;
};
The settings member is const as a hint that it should not be changed once a struct Foo has been created and initialized.
And I want to dynamically allocate this struct, e.g.
struct Foo * new_foo(void)
{
struct Foo *n = malloc(sizeof *n);
if (n) {
n->settings.a = SETTING_A;
n->settings.b = SETTING_B;
...
}
return n;
}
Now, this will not compile due to settings being const. What is a proper way to
initialize such a struct in this manner? Or is it better to not declare the settings member as const?
The memory is allocated (and thus, not constant), so it is legal to cast const away:
struct Foo * new_foo(void)
{
struct Foo *n = malloc(sizeof *n);
if (n) {
struct Params *s = (void *)&n->settings;
s->a = SETTING_A;
s->b = SETTING_B;
}
return n;
}
Here is one way to do it:
struct Foo *new_foo(void)
{
static struct Foo foo =
{
.settings =
{
.a = SETTING_A,
.b = SETTING_B
},
.state = ...
};
struct Foo *n = malloc(sizeof *n);
memcpy(n, &foo, sizeof *n);
return n;
}
Here is another way to do it:
struct Foo *new_foo(void)
{
static struct Params settings =
{
.a = SETTING_A,
.b = SETTING_B
};
struct Foo *n = malloc(sizeof *n);
memcpy((struct Params*)&n->settings, &settings, sizeof settings);
n->state = ...;
return n;
}

How can I store a function pointer in a structure?

I have declared typedef void (*DoRunTimeChecks)();
How do I store that as a field in a struct? How do I assign it? How do I call the fn()?
Just put it in like you would any other field:
struct example {
int x;
DoRunTimeChecks y;
};
void Function(void)
{
}
struct example anExample = { 12, Function };
To assign to the field:
anExample.y = Function;
To call the function:
anExample.y();
#include <stdio.h>
typedef void (*DoRunTimeChecks)();
struct func_struct {
DoRunTimeChecks func;
};
void function()
{
puts("hello");
}
int main()
{
struct func_struct func_struct;
func_struct.func = function;
func_struct.func();
return 0;
}

Resources