c - 错误 : deferencing pointer to incomplete type

标签 c

我经常看到这个问题/错误,但我就是想不通哪里出了问题。我创建了一个结构,其中包含有关线程的一些信息,基本上我想在每次创建线程时分配一些值(线程号、当前线程...等)。线程与此错误无关。到目前为止,我只是想让我的结构工作,但我一直收到一个错误,因为

部分的引用指针指向不完整的类型

thread->num=num;

是不是内存分配错误?或者有什么线索? 这是我的代码:

main.c//没有给我任何错误

struct sPRIME_THREAD *new_thread = create_thread(1,0,0,20);
printf("Thread Info:\n");
print_info(new_thread);

标题.h

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

/* Macro definitions */
#define MAX_THREADS     5   // Maximum number of prime search threads

/* Data types */
typedef struct              // Prime search thread data
{
    unsigned int num;       // Prime search thread number
    unsigned int current;   // Number currently evaluating for primality
    unsigned int low;       // Low end of range to test for primality
    unsigned int high;      // High end of range to test for primality
} sPRIME_THREAD;

/* Shared global variables */
extern sPRIME_THREAD primeThreadData[MAX_THREADS];  // Prime search thread data
int numThreads;                                     // Number of prime search threads

struct sPRIME_THREAD *create_thread(unsigned int num, unsigned int current,
                                    unsigned int low, unsigned int high) 
    {
        struct sPRIME_THREAD *thread = (sPRIME_THREAD *)malloc(sizeof(sPRIME_THREAD));

        thread->num = num;
        thread->current = current;
        thread->low = low;
        thread->high = high;

        return thread;
    }

void destroy_thread(struct sPRIME_THREAD *thread) 
{
    free(thread);
}

void print_info(struct sPRIME_THREAD *thread) 
{
    printf("Num: %d\n", thread->num);
    printf("Current: %d\n", thread->current);
    printf("Low: %d\n", thread->low);
    printf("High: %d\n", thread->high); 
}

最佳答案

struct sPRIME_THREAD 未定义。通过执行 typedef struct {...} sPRIME_THREAD;,您只需将匿名 struct 定义为类型 sPRIME_THREAD

要解决此问题,请删除 typedef

typedef struct              
{
  unsigned int num;       
  unsigned int current;  
  unsigned int low;       
  unsigned int high;     
} sPRIME_THREAD;

并定义一个struct类型

struct sPRIME_THREAD             
{
  unsigned int num;      
  unsigned int current;  
  unsigned int low;       
  unsigned int high;     
};

并且在引用类型时始终使用 struct sPRIME_THREAD

关于c - 错误 : deferencing pointer to incomplete type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36170799/

相关文章:

c - 如何在 SDL 2.0 绘制点、线或矩形中指定 "width"或 "point size"

c - 我怎样才能编译和运行这个项目?

javascript - 在网络浏览器上显示保存的文件

c - 为什么这个内存阶乘函数返回错误答案!

c++ - mq_notify 只启动一个线程

c - 在 C 中使用 "fscanf"如何拆分两个字符串?

c - 三个传感器,带 2 个 RGB LED Arduino

c - JACK 语言的 Lexer 中的段错误

c - 多线程启动顺序

c - 依靠 strtok_r 的内部指针安全吗?