简体   繁体   English

c-指向结构内部结构的空指针

[英]c - void pointer to struct inside a struct

EDIT: 编辑:

the exact error I get is : error: request for member 'q' in something not a structure or union 我得到的确切错误是:错误:在不是结构或联合的内容中请求成员“ q”

I corrected the typos I left in the code. 我更正了我在代码中留下的错别字。 It happened while formatting it for SO(camel case..). 在将其格式化为SO(驼峰盒..)时发生了。

context 语境

Problem to set a void pointer to a struct. 设置指向结构的空指针的问题。

My initial goal : I would like to point to a structure from a void pointer. 我的最初目标是 :我想从空指针指向结构。 pointMe.a will point to pointMe2 so that I can set pointMe2.q with an integer. pointMe.a将指向pointMe2,这样我就可以用整数设置pointMe2.q。

My final goal : being able to cast that void pointer to anything, while reusing my pointMe structure. 我的最终目标是 :在重用我的pointMe结构的同时,能够将空指针投射到任何对象上。 Maybe I could point to a structure and soon after to a char or an integer. 也许我可以指向一个结构,然后再指向一个char或一个整数。 Polymorphism I think. 我认为是多态的。

Failure 失败

Apparently, in the 3) of the code below, q is not part of a struct or a union. 显然,在下面的代码3)中,q不是结构或联合的一部分。 Looking at the pointers addresses, i know I am close but not there yet. 看着指针的地址,我知道我已经很近了,但是还没有到那儿。

My funky code 我的时髦代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {

    void * a;

}pointMe;

typedef struct {

    int q;

}pointMe2;

int main(void)
{

    pointMe arrow;
    pointMe2 * flesh;

    flesh = malloc(sizeof(pointMe2));
    flesh->q = 4;

    printf("1)\n Value: %d Address: %p\n",flesh->q,flesh );

    arrow.a = flesh;
    printf("2)\n arrow.a's address: %p flesh's address: %p\n",arrow.a,flesh );

    printf("3)\n arrow.a's address: %p Value of q : %d\n",arrow.a, *(arrow.a)->q );

    free(flesh);
    return 0;
}
printf("3)\n arrow.a's address: %p Value of q : %p\n",arrow.a, *(arrow.a)->q );

The .a member is a void pointer. .a成员是一个void指针。 It is impossible to dereference a void pointer because there is no such thing as a void type. 取消引用void指针是不可能的,因为不存在void类型。 You have to cast the pointer to the correct type first: 您必须先将指针强制转换为正确的类型:

printf("3)\n arrow.a's address: %p Value of q : %p\n",
       arrow.a,
       ((pointMe2 *)arrow.a)->q);

Note also that the %p conversion specifier expects a void pointer to be passed. 另请注意, %p转换说明符期望传递一个void指针。 When printing the value of flesh you need to cast it to void * before passing it to printf : 当打印flesh的值时,需要将其转换为void *然后printf

printf("1)\n Value: %d Address: %p\n", flesh->q, (void *)flesh );

There are some errors [typos i think] in your code. 您的代码中有一些错误[我认为是错别字]。

  1. pointme arrow; should be pointMe arrow; 应该是pointMe arrow; and so on. 等等。 [Modified in below code] [在下面的代码中修改]
  2. value of should be the value [ int type], so use %d with printf() . value of应为[ int类型]的值,因此将%dprintf()

Check the below code 检查以下代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {

    void * a;

}pointMe;

typedef struct {

    int q;

}pointMe2;

int main(void)
{

    pointMe arrow;            //corrected
    pointMe2 * flesh;         //corrected

    flesh = malloc(sizeof(pointMe2));
    flesh->q = 4;

    printf("1)\n Value: %d Address: %p\n",flesh->q,flesh );

    arrow.a = flesh;
    printf("2)\n arrow.a's address: %p flesh's address: %p\n",arrow.a,flesh );

    printf("3)\n arrow.a's address: %p Value of q : %d\n",arrow.a, ((pointMe2 *)(arrow.a))->q );  //corrected

    free(flesh);
    return 0;
}

使用*(arrow.a).q(arrow.a)->q

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

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