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);