Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 赋值中的不兼容类型_C_Incomplete Type - Fatal编程技术网

C 赋值中的不兼容类型

C 赋值中的不兼容类型,c,incomplete-type,C,Incomplete Type,我正在尝试对一组数组进行洗牌,并按洗牌顺序打印它们,但我遇到了错误:当从类型“IRIS”分配给类型“int”时,类型不兼容,我无法克服它 我是一个初级程序员(刚刚在过去的一周里为大学考试学习了一些基本的C语言) 这是我的代码: #include <stdio.h> #include <stdlib.h> #include <math.h> #include <time.h> #define MAX_SAMPLES 5 #define OUTPUT

我正在尝试对一组数组进行洗牌,并按洗牌顺序打印它们,但我遇到了
错误:当从类型“IRIS”分配给类型“int”时,类型不兼容
,我无法克服它

我是一个初级程序员(刚刚在过去的一周里为大学考试学习了一些基本的C语言)

这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define MAX_SAMPLES 5
#define OUTPUT 3
typedef struct {
  double sepal_lenght;
  double sepal_width;
  double petal_lenght;
  double petal_width;
  double out[OUTPUT];
}IRIS;
IRIS samples[MAX_SAMPLES] = {
{5.1,2.5,3.0,1.1,{0.0,1.0,0.0}},
{6.7,3.1,5.6,2.4,{1.0,0.0,0.0}},
{6.4,3.1,5.5,1.8,{1.0,0.0,0.0}},
{7.6,3.0,6.6,2.1,{1.0,0.0,0.0}},
{7.7,2.8,6.7,2.0,{1.0,0.0,0.0}},
};
main (){
int i, temp, randomIndex;
srand(time(NULL));
  for ( i=1; i < MAX_SAMPLES; i++) {
   temp = samples[i];
   randomIndex = rand() %MAX_SAMPLES;
   samples[i] = samples[randomIndex];
   samples[randomIndex] = temp;
}
for ( i=0; i<MAX_SAMPLES; i++) {
  printf("%d\n", samples[i]);
}
}
#包括
#包括
#包括
#包括
#定义最大样本数5
#定义输出3
类型定义结构{
重瓣萼片长;
重瓣萼片;
双瓣长;
双瓣宽;
双输出[输出];
}虹膜;
虹膜样本[最大样本数]={
{5.1,2.5,3.0,1.1,{0.0,1.0,0.0}},
{6.7,3.1,5.6,2.4,{1.0,0.0,0.0}},
{6.4,3.1,5.5,1.8,{1.0,0.0,0.0}},
{7.6,3.0,6.6,2.1,{1.0,0.0,0.0}},
{7.7,2.8,6.7,2.0,{1.0,0.0,0.0}},
};
主要(){
int i、temp、随机指数;
srand(时间(空));
对于(i=1;i对于(i=0;i您的
temp
类型为
int
,而
samples
IRIS
的数组,因此您需要
temp
成为
IRIS
,然后复制
samples[i]的所有内容
进入循环。

从循环前的
int
s列表中删除
temp
,然后更改:

temp = samples[i];
致:


您不能将
IRIS
类型的值分配给
int
您正在声明一个IRIS类型数组,但在for循环中您使用的是%d,该值仅用于在printf中显示int。 如果要显示结构虹膜中的数据,必须访问要显示的属性,而不是结构本身。 例如:

printf("%f\n", samples[i].sepal_lenght);

你将
int
分配给
IRIS
,这是不兼容的类型。这就是一切。
temp
int
类型;
samples[i]
IRIS
类型
length
应该是
length
非常感谢!这就解决了!(我学到了一些新东西)非常感谢!你的建议非常有用!
printf("%f\n", samples[i].sepal_lenght);