Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何修复错误:返回类型';结构{aka struct<;anonymous>;}_C_Visual Studio_Sorting_Random_Struct - Fatal编程技术网

如何修复错误:返回类型';结构{aka struct<;anonymous>;}

如何修复错误:返回类型';结构{aka struct<;anonymous>;},c,visual-studio,sorting,random,struct,C,Visual Studio,Sorting,Random,Struct,我正在编写一个返回随机值结构的代码。我不断得到错误:返回类型'student{aka struct}'时类型不兼容,但'struct student*'应返回s 我尝试了很多方法,在互联网上到处找,但我找不到解决办法 任何帮助都将不胜感激 #include <stdio.h> #include <string.h> #include <stdlib.h> #include <time.h> int method = 3; //if 0, orga

我正在编写一个返回随机值结构的代码。我不断得到错误:返回类型'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;
}
#包括
#包括
#包括
#包括
int方法=3//如果为0,则按全部组织。如果为1,则按年级组织。如果是3,按速度组织。
字符优先[40];
最后一个字符[25];
char*genName();
结构student*genStudent();
类型定义结构
{
字符名[60];
智力得分;
双倍时间;
}学生;
字符名[][15]=
{
{“约翰”},
{“吉姆”},
{“蒂姆”},
{“山姆”},
{“杰克”},
{“拉里”},
{“鲍勃”},
{“麦克”},
{“凯尔”},
{“汤姆”},
{“乔”},
{“丹”}
};
char lastNames[][25]=
{
{“霍华德”},
{“奈尔斯”},
{“琼斯”},
{“白色”},
{“迈尔斯”},
{“西蒙斯”},
{“史密斯”},
{“约翰逊”},
{“威廉姆斯”},
{“布朗”},
{“杰克逊”},
{“马丁”},
{“戴维斯”},
{“汤普森”},
{“摩尔”},
{“威克”},
};
整数比较(常数无效*,常数无效*);
学生s1={“吉姆·霍华德”,89,4.90};
学生s2={“Tim Nyles”,76,6.12};
学生s3={“约翰·琼斯”,97,7.56};
学生s4={“Sam White”,50,1.12};
学生s5={“杰克·迈尔斯”,88,3.90};
int main()
{
学生数组[]={s1,s2,s3,s4,s5};
qsort(array,(sizeof(array)/sizeof(array[0])),sizeof(array[0]),compare;
if(方法==1)
{
对于(int i=0;i<(sizeof(数组)/sizeof(数组[0]);i++)
{
printf(“%d.%s,分数为%d%%\n”,i+1,数组[i]。名称,数组[i]。分数);
}    
}
其他的
{
if(方法==2)
{
对于(int i=0;i<(sizeof(数组)/sizeof(数组[0]);i++)
{
printf(“%d.%s,时间为%1.2f分钟\n”,i+1,数组[i]。名称,数组[i]。时间);
}    
}
其他的
{
如果(方法==3)
{
对于(int i=0;i<(sizeof(数组)/sizeof(数组[0]);i++)
{
printf(“%d.%s,分数为%d%%,时间为%1.2f\n”,i+1,数组[i]。名称,数组[i]。分数,数组[i]。时间);
}
}
}
}
char*hi=genName();
printf(“%s”,hi);
返回0;
}
整数比较(常数void*num1,常数void*num2)
{
学生*st1=(学生*)num1;
学生*st2=(学生*)num2;
if(方法==1)
{
如果(st1->分数分数)
{
返回1;
}
如果(st1->score==st2->score)
{
返回0;
}
如果(st1->分数>st2->分数)
{
返回-1;
}
}
其他的
{
if(方法==2)
{
如果(st1->time>st2->time)
{
返回1;
}
如果(st1->time==st2->time)
{
返回0;
}
如果(st1->时间时间)
{
返回-1;
}   
} 
其他的
{
如果(方法==3)
{
如果(st1->分数分数)
{
返回1;
}
如果(st1->score==st2->score)
{
如果(st1->time>st2->time)
{
返回1;
}
如果(st1->time==st2->time)
{
返回0;
}
如果(st1->时间时间)
{
返回-1;
}  
}
如果(st1->分数>st2->分数)
{
返回-1;
}   
} 
}
}
}
char*genName()
{
srand(时间(0));
int r=rand()%(sizeof(firstNames)/sizeof(firstNames[0]);
strcpy(名字,名字[r]);
int v=rand()%(sizeof(lastNames)/sizeof(lastNames[0]);
srand(时间(0));
strcpy(姓氏[v]);
strcat(第一个、最后一个);
先返回;
}
结构学生*genStudent()
{
学生证;
srand(时间(0));
int sScore=rand()%101;
srand(时间(0));
双刺激=(双)兰德()/(10);
char*sName=genName();
strcpy(s.name,sName);
s、 分数=sScore;
s、 时间=时间;
返回s;
}

问题在于,您的
genStudent()
函数被声明/定义为返回指向
student
结构的指针,但实际上返回的是
student
对象。您可以按如下方式修复代码:

student* genStudent()
{
    student *s = malloc(sizeof(student)); // Allocate memory for a new object
    srand(time(0));
    int sScore = rand() % 101;
    srand(time(0));
    double sTime = (double)rand()/(10);
    char* sName = genName();
    strcpy(s->name, sName); // Change the "." to "->" as s is now a pointer
    s->score = sScore;
    s->time = sTime;
    return s;
}
当然,在程序后面的某个时候,您应该确保使用
free()
释放分配的内存

编辑:好的,要解决任何混淆,如果您有
student
类型的定义和
genStudent
的预声明,您需要更改代码:

struct student* genStudent();

typedef struct
{
    char name[60];
    int score;
    double time;
} student;
为此:

typedef struct
{
    char name[60];
    int score;
    double time;
} student;

student* genStudent();

请随时询问进一步的说明和/或解释。

函数
struct student*genStudent()
不返回
struct
,而是指向结构的指针,因此
student s。。。返回s不正确。此外,无论如何都不能返回指向局部变量的指针。因此,选择是,通过从函数定义中删除
*
返回
结构,或者,为
struct
分配动态内存并返回指向该结构的指针。请阅读并生成一个强调最小值的。此外,您还有
typedef
ed
student
,因此您应该使用
student
而不是
struct student
。实际上,我看不到在代码中调用
genStudent
函数的任何地方。另一件事:函数
compare()
不会从所有控制路径返回值。编译器警告是您的朋友。
struct student
也不正确,它尚未定义。@WeatherVane抱歉,键入我的代码太快(还有一些其他错误)。使用
struct student
确实会编译,但会出现警告。MSVC给出了一个实际错误:genStudent使用未定义的struct student。好吧,在Visual Studio 2019中使用MSVC,添加
struct
关键字(在
genStudent
的定义中)会编译带有以下奇怪警告:
warning C4133: