错误:使用类型'taus88_t'|初始化类型'struct taus88_t *'时出现不兼容的类型?

时间:2013-07-27 02:58:24

标签: c gcc types incompatibility

我正在尝试使用GCC编译以下C程序,我收到了一个错误 在第七行,因为类型taus88_t*在某种程度上无法从名为make_taus88(seed);的初始化函数调用中正确返回?

error: incompatible types when initializing type 'struct taus88_t *' using type 'taus88_t'|

我已尝试使用taus88_t TAUS88 = make_taus88(6346456);,但这会产生更多错误/警告。

taus88main.c||In function 'main':|
taus88main.c|8|error: incompatible type for argument 1 of 'taus88u32'|
taus88.h|21|note: expected 'struct taus88_t *' but argument is of type 'taus88_t'|
taus88main.c|9|error: incompatible type for argument 1 of 'taus88f32'|
taus88.h|22|note: expected 'struct taus88_t *' but argument is of type 'taus88_t'|
taus_88_cpp\taus88main.c|9|warning: unused variable 'numberf32'|
taus_88_cpp\taus88main.c|8|warning: unused variable 'numberu32'|

该项目位于以下3个文件中。

taus88main.c

#include "taus88.h"
#include <stdint.h>

int main()
{

    taus88_t* TAUS88 = make_taus88(6346456);
    u32 numberu32 = taus88u32(TAUS88);
    f32 numberf32 = taus88f32(TAUS88);

    return 0;
}

taus88.h

#ifndef _COMMON_TAUS88_H
#define _COMMON_TAUS88_H

#include <stdint.h>
typedef int8_t    i8;
typedef int16_t   i16;
typedef int32_t   i32;
typedef int64_t   i64;

typedef uint8_t   u8;
typedef uint16_t  u16;
typedef uint32_t  u32;
typedef uint64_t  u64;
typedef float  f32;
typedef double f64;

typedef struct {u32 s1, s2, s3;} taus88_t;

taus88_t make_taus88(u32 seed);
u32 taus88u32(taus88_t *t);
f32 taus88f32(taus88_t *t);

#endif

taus88.c

#include <stdint.h>
#include "taus88.h"

taus88_t make_taus88(u32 seed)
{
  taus88_t t;
  t.s1 = 1243598713U ^ seed; if (t.s1 <  2) t.s1 = 1243598713U;
  t.s2 = 3093459404U ^ seed; if (t.s2 <  8) t.s2 = 3093459404U;
  t.s3 = 1821928721U ^ seed; if (t.s3 < 16) t.s3 = 1821928721U;
  return t;
}

u32 taus88u32(taus88_t *t)
{
  t->s1 = ((t->s1 &  -2) << 12) ^ (((t->s1 << 13) ^  t->s1) >> 19);
  t->s2 = ((t->s2 &  -8) <<  4) ^ (((t->s2 <<  2) ^  t->s2) >> 25);
  t->s3 = ((t->s3 & -16) << 17) ^ (((t->s3 <<  3) ^  t->s3) >> 11);
  return t->s1 ^ t->s2 ^ t->s3;
}

f32 taus88f32(taus88_t *t)
{
  union {u32 i ; f32 f ;} u;
  u.i = 0x3F800000 | (taus88u32(t) >> 9);
  return u.f - 1.0;
}

3 个答案:

答案 0 :(得分:4)

所以这里的主要问题是你返回一个taus8_t并试图将它分配给一个无效的taus88_t *,如果你需要使用指针,原因是那些不明显的原因。代码然后修复如下:

taus88_t* TAUS88 = malloc(sizeof(taus88_t)) ;

*TAUS88 = make_taus88(6346456);

当你完成时,你必须记得在指针上调用free。一种更简单的方法是跳过使用指针并执行如下操作:

taus88_t TAUS88 ;

TAUS88 = make_taus88(6346456);
u32 numberu32 = taus88u32(&TAUS88);
f32 numberf32 = taus88f32(&TAUS88);

现在您不必担心再次调用free

我指出的另一个问题是,taus88f32很可能会违反strict aliasing规则。

答案 1 :(得分:0)

如何将taus88_t* TAUS88 = make_taus88(6346456);分成两行,如

taus88_t* TAUS88;
*TAUS88 = make_taus88(6346456);

http://ideone.com/KVbf79

答案 2 :(得分:0)

当您尝试将结果分配给make_taus88(指向taus88_t的指针)时,taus88_t *会返回taus88_t

所以,第一个大问题是make_taus88在本地定义t,然后尝试返回它。您不能这样做,因为当make_taus88终止时,t将超出范围。

第二个是你要归还taus88_t而不是taus88_t *

您可以通过在t中将taus88_t *声明为make_taus88并使用malloc之类的内容动态初始化来解决这两个问题。然后,您可以返回t,它将是正确的类型,并在堆上分配,这样当make_taus88超出范围时,内存仍然可用。

请注意,在make_taus88中,您还必须将表单t.s1的所有引用更改为t->s1