无法在指向结构的指针中将:: c*转换为c*

use*_*719 2 c struct pointers

这是摘要:

struct a
{
    struct b{
    } x;

    struct c{
    } y;

};

main(){
    struct a z;
    fun(&z.y)
}
fun(struct c *p){
}
Run Code Online (Sandbox Code Playgroud)

在函数中传递参数时,从:: c*到c*的类型转换中出错.

我的问题是初学者是否可以参与stackoverflow,因为似乎我的问题更有可能获得负分数.

原始程序是:

#include<stdio.h>
#include<conio.h>

void fun(struct c *p);
struct a
{
    struct b
    {
        int i;
        float f;
        char ch;
    } x;


    struct c
    {
        int j;
        float g;
        char ch;
    } y;
};

void main()
{
    int *p;
    struct a z;
    clrscr();
    fun(&z.y);
    printf("\n%d %f %c",z.x.i,z.x.f,z.x.ch) ;
    getch();
}
void fun(struct c *p)
{
    int offset;
    struct b *address;
    offset= (char *) &((struct c *)(& ((struct a*)0)->y)->j)-(char *)((struct a*)0);
    address =(struct b *)((char *)& (p->j)-offset);
    address->i=400;
    address->f=3,14;
    address->char='s';     
}
Run Code Online (Sandbox Code Playgroud)

还有一个问题0是如何进行类型转换((struct a *)0).

Yu *_*Hao 5

该错误是因为您使用C++编译器来编译C代码.

C代码很好,除了你应该fun在调用它之前添加函数的声明(就像你在完整程序中所做的那样)并使用它int main.

对于第二个问题,0这里不是一个整数,而是一个空指针,((struct a *)0)是一个转换为类型的空指针struct a*.