Я делаю код, который возвращает структуру со случайными значениями. Я получаю сообщение об ошибке: несовместимые типы при возврате типа 'student {aka struct}', но ожидалось возвращение 'struct student *' s;
Я много чего пробовал и просмотрел весь интернет, но яне может найти решение.
Любая помощь будет оценена.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
int method = 3; //if 0, organize by all. if 1, organize by grade. if 3, organize by speed.
char first[40];
char last[25];
char* genName();
struct student* genStudent();
typedef struct
{
char name[60];
int score;
double time;
} student;
char firstNames[][15] =
{
{"John"},
{"Jim"},
{"Tim"},
{"Sam"},
{"Jack"},
{"Larry"},
{"Bob"},
{"Mack"},
{"Kyle"},
{"Tom"},
{"Joe"},
{"Dan"}
};
char lastNames[][25] =
{
{" Howards"},
{" Nyles"},
{" Jones"},
{" White"},
{" Myles"},
{" Simones"},
{" Smith"},
{" Johnson"},
{" Williams"},
{" Brown"},
{" Jackson"},
{" Martin"},
{" Davis"},
{" Thompson"},
{" Moore"},
{" Wick"},
};
int compare(const void*, const void*);
student s1 = {"Jim Howards", 89, 4.90};
student s2 = {"Tim Nyles", 76, 6.12};
student s3 = {"John Jones", 97, 7.56};
student s4 = {"Sam White", 50, 1.12};
student s5 = {"Jack Myles", 88, 3.90};
int main()
{
student array[] = {s1, s2, s3, s4, s5};
qsort(array, (sizeof(array)/sizeof(array[0])), sizeof(array[0]), compare);
if (method == 1)
{
for (int i = 0; i < (sizeof(array)/sizeof(array[0])); i++)
{
printf("%d.%s, with a score of %d%%\n", i+1, array[i].name, array[i].score);
}
}
else
{
if (method == 2)
{
for (int i = 0; i < (sizeof(array)/sizeof(array[0])); i++)
{
printf("%d.%s, with a time of %1.2f minutes\n", i+1, array[i].name, array[i].time);
}
}
else
{
if (method == 3)
{
for (int i = 0; i < (sizeof(array)/sizeof(array[0])); i++)
{
printf("%d.%s, with a score of %d%%, and a time of %1.2f\n", i+1, array[i].name, array[i].score, array[i].time);
}
}
}
}
char* hi = genName();
printf("%s", hi);
return 0;
}
int compare(const void * num1, const void * num2)
{
student *st1 = (student *)num1;
student *st2 = (student *)num2;
if (method == 1)
{
if (st1->score < st2->score)
{
return 1;
}
if (st1->score == st2->score)
{
return 0;
}
if (st1->score > st2->score)
{
return -1;
}
}
else
{
if (method == 2)
{
if (st1->time > st2->time)
{
return 1;
}
if (st1->time == st2->time)
{
return 0;
}
if (st1->time < st2->time)
{
return -1;
}
}
else
{
if (method == 3)
{
if (st1->score < st2->score)
{
return 1;
}
if (st1->score == st2->score)
{
if (st1->time > st2->time)
{
return 1;
}
if (st1->time == st2->time)
{
return 0;
}
if (st1->time < st2->time)
{
return -1;
}
}
if (st1->score > st2->score)
{
return -1;
}
}
}
}
}
char* genName()
{
srand(time(0));
int r = rand() % (sizeof(firstNames)/sizeof(firstNames[0]));
strcpy(first, firstNames[r]);
int v = rand() % (sizeof(lastNames)/sizeof(lastNames[0]));
srand(time(0));
strcpy(last, lastNames[v]);
strcat(first, last);
return first;
}
struct student* genStudent()
{
student s;
srand(time(0));
int sScore = rand() % 101;
srand(time(0));
double sTime = (double)rand()/(10);
char* sName = genName();
strcpy(s.name, sName);
s.score = sScore;
s.time = sTime;
return s;
}