Salvadi
Salvadi

Reputation: 15

incompatible type in assignement

i'm trying to shuffle a set of arrays and print them in the shuffled order, but i get the error: incompatible types when assigning to type 'int' from type 'IRIS' and i can't overcome it.

I'm a beginner programmer (just learned some basic C over past week for a university exam).

this is my code :

#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]);
}
}

error in line: temp = samples[i];

any help very appriciated!

Upvotes: 1

Views: 71

Answers (3)

Shiho
Shiho

Reputation: 116

You are declaring an IRIS type array but in your for loop you are using %d which are used for displaying only int into printf. If you want to display data from your structure IRIS, you must access the attribute you want to display, not the structure itself. For example:

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

Upvotes: 1

unwind
unwind

Reputation: 400129

Remove temp from the list of ints before the loop, then change this:

temp = samples[i];

to:

const IRIS temp = samples[i];

You can't assign a value of type IRIS to an int.

Upvotes: 2

ForceBru
ForceBru

Reputation: 44926

Your temp is of type int while samples is an array of IRIS, so you need temp to be IRIS and then copy all the contents of samples[i] into it.

Upvotes: 0

Related Questions