如何解决错误:返回类型'struct {aka struct }

问题描述 投票:-1回答:1

我正在编写一个返回具有随机值的结构的代码。我不断收到错误消息:返回类型'student {aka struct}'时出现不兼容的类型,但是'struct student *'被期望返回s;

我已经尝试了很多事情,并且遍及整个Internet,但是找不到解决方案。

任何帮助都会感激。

#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;
}
c visual-studio sorting random struct
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.