取消引用指向不完整类型“struct ____"的指针 [英] Dereferencing pointer to incomplete type 'struct ____'

查看:44
本文介绍了取消引用指向不完整类型“struct ____"的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 C 中创建一种可以接受大多数原始类型的类型.我是 C 新手,不太了解结构.我的错误发生在第 10 行(main.c),如果删除第 10 行(也是 main.c),它也会发生在第 11 行.如果有人有想法/指示,我将不胜感激!非常感谢!

I am attempting to create a type in C that can accept most primitive types. I am new to C and don't understand structs very well. My error occurs on line 10 (of main.c) and it will also occur on line 11 if line 10 is removed (also of main.c). If anyone has an ideas/pointers I would really appreciate it! Many thanks!

main.c:

#include <stdio.h>

#include "modularType.h"

int main()
{
    int data = 1;

    PyType object = createPyObjectInt(data);
    printf("Type of data:  %d\n", object->typeOfData);
    printf("Value of data: %d\n", object->intData);

    return 0;
}

modularType.h:

modularType.h:

typedef struct pytype *PyType;

PyType createPyObjectEmpty(void);
PyType createPyObjectInt(int data);

void setDataInt(PyType, int data);
void setDataIntStar(PyType, int* data);

void freeMyType(PyType);
void freeCharStar(PyType);
void freeIntStar(PyType);
void freeFloatStar(PyType);

modularType.c:

modularType.c:

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

#ifndef NEW_TYPE
#define NEW_TYPE

#include "modularType.h"

typedef enum
{
    NO_DATA,
    REG_CHAR,
    CHAR_STAR,
    REG_INT,
    INT_STAR,
    REG_FLOAT,
    FLOAT_STAR,
}types;

struct pytype
{
    //The number of data types i can hold here
    unsigned char typeOfData: 3;

    union
    {
        char   charData;
        char*  charStarData;
        int    intData;
        int*   intStarData;
        float  floatData;
        float* floatStarData;
    }
};

PyType createPyObjectEmpty(void)
{
    PyType object;

    object = malloc(sizeof(*object));

    object->typeOfData = NO_DATA;

    return object;
}

PyType createPyObjectInt(int data)
{
    PyType object;

    object = malloc(sizeof(*object));

    object->intData = data;
    object->typeOfData = REG_INT;

    return object;
}

void setDataInt(PyType object, int data)
{
    object->intData    = data;
    object->typeOfData = REG_INT;
}

void setDataIntStar(PyType object, int* data)
{
    object->intStarData = data;
    object->typeOfData  = INT_STAR;
}

#endif

作为旁注,我的编译命令 (gcc -Wall -std=c99 -o modType main.c modulesType.c) 产生以下警告:modularType.c:35:1: 警告:末尾没有分号结构或联合.我认为我已经正确格式化了结构,但我也看到人们将结构定义如下:

As a side note my compilation command (gcc -Wall -std=c99 -o modType main.c modularType.c) produces the following warning: modularType.c:35:1: warning: no semicolon at end of struct or union. I thought that I had formatted my struct correctly but I have also seen people define structs as following:

typedef struct pytype
{
    //code here
}PyType;

这是更好的方法还是我的方法很好?

Is this a better way or is the way I am doing it fine?

推荐答案

头文件中的结构体定义不完整,因此您无法在代码中引用任何结构体成员.编译器抱怨它根本不知道任何结构体成员.

The structure definition is incomplete in the header file, so you cannot refer to any struct members in your code. The compiler complains that it does not know about any struct members at all.

不完整 结构定义是指您不提供实现成员列表的结构定义.这样的定义允许您操作指向此类结构的指针,但不能访问任何成员,因为它们没有明确定义,并且编译器需要知道它们的类型和从结构开头的偏移量才能生成相应的代码.

An incomplete structure definition is one where you do not provide the list of members for the implementation. Such a definition allows you to manipulate pointers to such structures, but not access any members since they are not explicitly defined and the compiler needs to know their type and offset from the beginning of the structure to generate the corresponding code.

也不是 typedef PyType 隐藏了一个指向 struct pytype 的指针.将指针隐藏在 typedef 后面很容易出错,从而导致程序员和读者混淆代码.

Also not that the typedef PyType hides a pointer to a struct pytype. It is error prone to hide pointers behind typedefs, leading to confusing code for both the programmer and the reader.

这篇关于取消引用指向不完整类型“struct ____"的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆