C 返回结构的结构和Int

C 返回结构的结构和Int,c,struct,return,C,Struct,Return,我试图使用这两个结构将一个结构返回到main,然后调用排序和打印等其他函数 我还收到了错误:请求使用非结构或联盟的成员“车辆” 与相同的错误一起出现,但对于错误:请求某个内容中的成员“计数”,而不是结构或联合 这些错误与 value.count = count; value.vehicles = vehicles; 这些是我正在使用的结构 struct data{ char *model; float engineSize; int cost; char *

我试图使用这两个结构将一个结构返回到main,然后调用排序和打印等其他函数

我还收到了
错误:请求使用非结构或联盟的成员“车辆”

与相同的错误一起出现,但对于
错误:请求某个内容中的成员“计数”,而不是结构或联合

这些错误与

value.count = count;
 value.vehicles = vehicles;
这些是我正在使用的结构

struct data{

    char *model;
    float engineSize;
    int cost;
    char *color;
};

struct values{

    struct data vehicles;
    int count;
};
这是将返回结构的sort readFile函数

struct values * readFile(){

    FILE *fp;
    int c;
    int count = 0;
    char *line = NULL;
    size_t len = 0;

    fp = fopen("hw3.data", "r");


    while ((c = fgetc(fp)) != EOF){
        if(c == '\n'){
            count++;
        }

    }

    if (feof(fp)){

        rewind(fp);

        struct data *vehicles = malloc((sizeof(struct data))* count);


        count = 0;
        char *token = NULL;
        while (getline(&line, &len, fp)!= -1){

            printf("%s", line);

            token = strtok(line,  " ");

            vehicles[count].model = malloc(strlen(token) + 1);
            strcpy(vehicles[count].model, token);

            token = strtok(NULL, " ");
            vehicles[count].engineSize = atof(token);

            token = strtok(NULL, " ");
            vehicles[count].cost = atoi(token);

            token = strtok(NULL, " ");
            vehicles[count].color = malloc(strlen(token) + 1);
            strcpy(vehicles[count].color, token);

            free(line);
            line = NULL;
            len = 0;

        }
        struct values *value = malloc(sizeof(struct values));
    value.vehicles = vehicles;
    value.count = count;
    return value;
    }
这是我将用来调用readFile函数、sort函数和print函数的主要函数

int main(){

    int check = 1;
    int input, n;

    while (check == 1){

        printf("Enter a value corresponding to a option on the menu below\n\n");

        printf("1. Sort data by the float value & print high to low\n");
        printf("2. Sort data by the float value & print low to high\n");
        printf("3. Sort data by the int value & print high to low\n");
        printf("4. Sort data by the int value & print low to high\n");
        printf("5. Exit\n\n");

        printf("Enter a value corresponding to the above menu\n");
        scanf("%d", &input);

        struct values *value = readFile();
        struct data *vehicles = value.vehicles;
        n = value.count;

        if(input == 1 || input == 2 || input == 3 || input == 4 || input == 5){

            if (input == 5){

                exit(0);

            }if (input == 1){

                //sort float high to low
                //bubbleSortFloats(vehicles[], 0);


            }if (input == 2){

                //sort float low to high
                //bubbleSortFloats(vehicles[], 1);


            }if (input == 3){


                //sort int value high to low
                //bubbleSortInts(vehicles[], 0);



            }if (input == 4){

                //sort int value low to high
                //bubbleSortInts(vehicles[], 1);


            }

        }else{

            printf("Enter a correct value for the menus above\n\n" );
        }



    }

}
会的

value->vehicles = vehicles
value
是指向
struct values
结构实例内存的指针。首先取消对指针的引用以获取结构实例。编译器抱怨,因为您试图从既不是结构也不是联合的指针变量获取
vehicles
属性-它是指向结构值实例的指针。其他成员的情况也一样
value->count=count

请记住,
value->count
(*value.count)相同。这方面的简写符号是箭头符号<代码>->

  • value
    -是指向
    struct values
    变量的指针
  • *值
    -是
    结构值
    变量
  • (*value).count
    -是结构实例
    结构值的成员变量
  • 值->计数
    -与最后一行相同
这将是

value->vehicles = vehicles
value
是指向
struct values
结构实例内存的指针。首先取消对指针的引用以获取结构实例。编译器抱怨,因为您试图从既不是结构也不是联合的指针变量获取
vehicles
属性-它是指向结构值实例的指针。其他成员的情况也一样
value->count=count

请记住,
value->count
(*value.count)相同。这方面的简写符号是箭头符号<代码>->

  • value
    -是指向
    struct values
    变量的指针
  • *值
    -是
    结构值
    变量
  • (*value).count
    -是结构实例
    结构值的成员变量
  • 值->计数
    -与最后一行相同

好的,那么你只是通过将struct values*value转换为struct values value来解除引用吗?@Jeffreyhenen:我不明白你的要求,但是你必须解除引用指针
value
-
(*value)。车辆
值->车辆
。那是it@JeffreyHennen.希望有帮助?如果有疑问,请提问好的,那么您是通过将结构值*值转换为结构值来解除引用吗?@Jeffreyhenen:我不明白您所问的,但您必须解除引用指针
--
(*值)。车辆
值->车辆
。那是it@JeffreyHennen.希望有帮助?如有疑问,请提问