gpt4 book ai didi

c - 错误 : deferencing pointer to incomplete type

转载 作者:太空宇宙 更新时间:2023-11-04 04:31:04 27 4
gpt4 key购买 nike

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

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

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com