【发布时间】:2016-01-14 12:13:04
【问题描述】:
我正在尝试打乱一组数组并按打乱顺序打印它们,但我得到了error: incompatible types when assigning to type 'int' from type 'IRIS',但我无法克服它。
我是一名初级程序员(过去一周刚为大学考试学习了一些基本的 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]);
}
}
行错误:temp = samples[i];
非常感谢任何帮助!
【问题讨论】:
-
您将
int分配给IRIS,这是不兼容的类型。就是这样。 -
temp是int类型;samples[i]的类型为IRIS -
lenght应该是length
标签: c incomplete-type