错误:引用指向不完整类型的指针

时间:2016-03-23 05:56:59

标签: 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);

header.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); 
}

2 个答案:

答案 0 :(得分:0)

如果您使用struct,则无需使用typedef关键字。请参阅typedef

检查这个

/* 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

sPRIME_THREAD *create_thread(unsigned int num, unsigned int current,
                                unsigned int low, unsigned int high)
{
   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(sPRIME_THREAD *thread)
{
free(thread);
}

 void print_info(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);
 }

答案 1 :(得分:0)

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

要修复此drop,请输入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