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

时间:2022-06-08 15:45:51

I'm trying to compile the following C program using GCC and I'm getting an error on line seven because the type taus88_t* is somehow not getting returned properly from the initializing function call called make_taus88(seed);?

我正在尝试使用GCC编译下面的C程序,在第7行我得到了一个错误,因为类型taus88_t*在名为make_taus88(seed)的初始化函数调用中没有得到正确的返回;

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

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

I've tried using taus88_t TAUS88 = make_taus88(6346456); but that gives more errors/warnings.

我试过用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'|

The project is in 3 files below.

项目在下面3个文件中。

taus88main.c

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

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

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 个解决方案

#1


4  

So the main problem here is that you are returning a taus8_t and trying to assign that to a taus88_t * which is not valid, if you need to use pointers for reasons that are not obvious from the code then the fix is as follows:

这里的主要问题是你返回一个taus8_t并试图将它赋值给一个taus88_t *这是无效的,如果你需要使用指针的原因在代码中并不明显,那么修正如下:

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

*TAUS88 = make_taus88(6346456);

You must remember to call free on the pointer though when you are done. A simpler approach, would be to skip using a pointer and do as follows:

您必须记住,当您完成时,在指针上调用free。一种更简单的方法是使用指针跳过,并按照以下步骤进行:

taus88_t TAUS88 ;

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

Now you don't have to worry about calling free anymore.

现在你不用再担心拨打免费电话了。

The other issue that I pointed out is that most likely taus88f32 violates strict aliasing rules.

我指出的另一个问题是,taus88f32很可能违反了严格的混叠规则。

#2


0  

It looks like make_taus88 returns a taus88_t while you're trying to assign the result to a taus88_t * (pointer to taus88_t)

看起来make_taus88返回一个taus88_t,而您正在尝试将结果分配给一个taus88_t *(指向taus88_t的指针)

So, the first big problem is the make_taus88 is defining t locally, then trying to return it. You can't do that, because when make_taus88 terminates, t will be out of scope.

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

The second is that you're returning a taus88_t and not a taus88_t *.

第二个是返回一个taus88_t,而不是taus88_t *。

You can fix both by declaring t as taus88_t * in make_taus88, and initializing it dynamically using something like malloc. Then, you can return t and it will be the correct type, and allocated on the heap, so that when make_taus88 goes out of scope, the memory will still be available.

您可以通过在make_taus88中将t声明为taus88_t *,并使用malloc之类的东西动态地初始化它来修复这两者。然后,您可以返回t,它将是正确的类型,并分配到堆上,以便当make_taus88超出范围时,内存仍然可用。

Note that in make_taus88 you'd also have to change all references of the form t.s1 to t->s1.

注意,在make_taus88中,您还必须更改形式t的所有引用。s1到t - > s1。

#3


0  

What about breaking taus88_t* TAUS88 = make_taus88(6346456); into two lines like

打破taus88_t* TAUS88 = make_taus88(6346456)怎么样?分成两行像

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

http://ideone.com/KVbf79

http://ideone.com/KVbf79

#1


4  

So the main problem here is that you are returning a taus8_t and trying to assign that to a taus88_t * which is not valid, if you need to use pointers for reasons that are not obvious from the code then the fix is as follows:

这里的主要问题是你返回一个taus8_t并试图将它赋值给一个taus88_t *这是无效的,如果你需要使用指针的原因在代码中并不明显,那么修正如下:

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

*TAUS88 = make_taus88(6346456);

You must remember to call free on the pointer though when you are done. A simpler approach, would be to skip using a pointer and do as follows:

您必须记住,当您完成时,在指针上调用free。一种更简单的方法是使用指针跳过,并按照以下步骤进行:

taus88_t TAUS88 ;

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

Now you don't have to worry about calling free anymore.

现在你不用再担心拨打免费电话了。

The other issue that I pointed out is that most likely taus88f32 violates strict aliasing rules.

我指出的另一个问题是,taus88f32很可能违反了严格的混叠规则。

#2


0  

It looks like make_taus88 returns a taus88_t while you're trying to assign the result to a taus88_t * (pointer to taus88_t)

看起来make_taus88返回一个taus88_t,而您正在尝试将结果分配给一个taus88_t *(指向taus88_t的指针)

So, the first big problem is the make_taus88 is defining t locally, then trying to return it. You can't do that, because when make_taus88 terminates, t will be out of scope.

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

The second is that you're returning a taus88_t and not a taus88_t *.

第二个是返回一个taus88_t,而不是taus88_t *。

You can fix both by declaring t as taus88_t * in make_taus88, and initializing it dynamically using something like malloc. Then, you can return t and it will be the correct type, and allocated on the heap, so that when make_taus88 goes out of scope, the memory will still be available.

您可以通过在make_taus88中将t声明为taus88_t *,并使用malloc之类的东西动态地初始化它来修复这两者。然后,您可以返回t,它将是正确的类型,并分配到堆上,以便当make_taus88超出范围时,内存仍然可用。

Note that in make_taus88 you'd also have to change all references of the form t.s1 to t->s1.

注意,在make_taus88中,您还必须更改形式t的所有引用。s1到t - > s1。

#3


0  

What about breaking taus88_t* TAUS88 = make_taus88(6346456); into two lines like

打破taus88_t* TAUS88 = make_taus88(6346456)怎么样?分成两行像

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

http://ideone.com/KVbf79

http://ideone.com/KVbf79