简体   繁体   English

试图将一个空指针投射到一个结构

[英]trying to cast a void pointer to a struct

I know this has been asked before but I never got an answer that suited me.. so here goes.. here is my project header file: 我知道这已经被问过了,但是我从来没有得到适合我的答案..所以这是我的项目头文件:

struct rss_s {
char * device_info;
char * device_model;
char * device_serial;
Radio_Types radio_type;
int power_48v;
int power_400hz;
int panel_lamps;
void * radio_info;
int sub_devices;
struct device_s {
    int fd[ FD_pair ];
    int frequency[ tuned ];
    int panel_lamp;
}sub_device[];
  };

and here is a struct for one of radios: 这是其中一个收音机的结构:

struct radio_614L8 {
loop_sw614L8 loop_sw_614L8;
mode_sw614L8 mode_sw_614L8;
int sw_band;
int sw_bfo;
int meter;
int dial_lamp;
  };

I initialized everything in main with the following: 我用以下命令初始化了main中的所有内容:

static struct radio_G3713 G_3713;
static struct radio_614L8 C_614L8;
static struct radio_G1981 G_1981;
static struct radio_G3490 G_3490;
static struct radio_G4214 G_4214;

static struct rss_s radios[] = {
{ "COM/NAV #1", "G-3717", "81",  G3717, 0, 0, 0, & G_3713,  2, },
{ "ADF",        "614L8", "8384", C614L8,0, 0, 0, & C_614L8, 1, },
{ "ATC",        "G-1981", "336", G1981, 0, 0, 0, & G_1981,  1, },
{ "5in1",       "G-3490", "31",  G3490, 0, 0, 0, & G_3490,  4, },
{ "COM/NAV #2", "G-4214", "68",  G4214, 0, 0, 0, & G_4214,  1, }};

the forth column is a enum: so by using a pointer to "radio -> radio_info" I now know which radio sub-system I need to use. 第四列是一个枚举:因此,通过使用指向“ radio-> radio_info”的指针,我现在知道我需要使用哪个无线电子系统。
the problem is I need to cast "void * radio_info" to a pointer type of "struct radio_C614L8" so I tried: 问题是我需要将“ void * radio_info”强制转换为“ struct radio_C614L8”指针类型,所以我尝试了:
radios -> radio_info = ( struct radio_614L8 *) radios -> radio_info; radios-> radio_info =(struct radio_614L8 *)radios-> radio_info;
and got statement with no effect.... 并得到没有效果的声明。

but all I get out of eclipse is an error message: 但是我从日食中得到的只是一条错误消息:
undefined reference to `init_C614L8' 未定义对“ init_C614L8”的引用

ok I give up what am I doing wrong.. please help 好吧,我放弃我在做什么错..请帮助

I think, if understand correctly now, you need a temporary variable. 我认为,如果现在正确理解,则需要一个临时变量。

Where you would like to use a radio_G3713, you should do something like: 您想使用radio_G3713的地方,应该执行以下操作:

   struct radio_G3713 *radio = ((struct radio_G3713 *)(radios[i]->radio_info);
   radio->meter++;
radios -> radio_info = ( struct radio_614L8 *) radios -> radio_info;

does nothing because radios->radio_info is still type void *. 什么也不做,因为radios-> radio_info仍为void *。

You need to put the result of your typecast in a variable of the right type 您需要将类型转换的结果放入正确类型的变量中

 struct radio_614L8 * r_614L8 = ( struct radio_614L8 *) radios -> radio_info;

for instance. 例如。

As I've stated as a comment, typecasting a variable creates only a temporary variable with the type you desire. 正如我所说的那样,对变量进行类型转换只会创建具有所需类型的临时变量。 You may not ever change the type of the variable you have already declared. 您可能永远不会更改已经声明的变量的类型。 For example, the following also won't work, and is truly analogous: 例如,以下内容也将不起作用,并且实际上是类似的:

int a = 5;
a = (float) a;

Here, in the first line, a variable of type int is declared, assigned with the value 5 . 在这里,在第一行中,声明了一个类型为int的变量,并分配了值5 Then with the second line, first the value 5 gets typecasted as float , which gives us a 5.0 . 然后在第二行中,首先将值5转换为float ,这使我们得到5.0 Then this 5.0 is to be assigned to a again, which happens to be an int , so it will get implicitly casted into an int first, into a 5 , then get assigned to a . 然后将此5.0再次分配给a恰好是int ,因此它将首先隐式转换为int ,转换为5 ,然后分配给a

a may never be a float , a is destined to be an int ever since it got declared. a可能永远不会是floata自被声明以来注定是一个int

I don't really know what you are hoping to have in the end, but here's something: 我真的不知道您最终希望得到什么,但是这里有一些东西:

By making a call like radios->radio_info , you refer to the radio_info inside the very first radio , which is the radio[0] , be aware of that. 通过发出类似于radios->radio_info的呼叫,您可以radio_info第一个radio (即radio[0]radio_info If you want to refer to the radio_info of a specific radio , then either use (radio + x)->radio_info or radio[x].radio_info , both are equivalent. 如果要引用特定radioradio_info ,则可以使用(radio + x)->radio_inforadio[x].radio_info ,两者都是等效的。

Second thing is, you first need to have a memory location that holds a struct radio_614L8 to be able to point to it. 第二件事是,您首先需要具有一个存储struct radio_614L8的存储位置才能指向它。 Two ways to do that: 有两种方法:

// declaring one and then assigning its address to your pointer
struct radio_614L8 asd;
(radio + 0)->radio_info = &asd;

// or allocating memory for one and assigning the address of the memory to your pointer
(radio + 0)->radio_info = malloc( sizeof( struct radio_614L8 ) );

After that point, you can refer to your void pointer, as if it was a struct radio_614L8 pointer with the following manner, and change its sw_band , meter , whatever: 之后,您可以按照以下方式引用您的void指针,就好像它是struct radio_614L8指针一样,并更改其sw_bandmeter或其他内容:

((struct radio_614L8 *) (radio + 0)->radio_info) -> meter = 2345;
((struct radio_614L8 *) (radio + 0)->radio_info) -> dial_lamp = 123;

I know, it's kind of long... But if you really want to keep the radio_info in your structure as a void * , then this is the way to go. 我知道,这有点长...但是,如果您真的想将radio_info保留为结构中的void * ,那么这就是要走的路。 What's happening there is: 发生了什么事:

(radio + 0)->radio_info;    // recognized as a void *
                            // has some allocated memory though...
(struct radio_614L8 *) (radio + 0)-> radio_info;
                            // recognized as a struct radio_614L8 * now
                            // allowing you to refer sw_bfo, etc.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM