查找结构数组的大小 C

Jef*_*nen 1 c arrays sorting size

我试图找到我用来对其进行排序的结构数组的大小。不确定如何使用通常在非结构数组中使用的 sizeof() 函数来实现它。

我还试图弄清楚在参数与临时结构变量中初始化车辆结构时我做错了什么。

error: incompatible types when initializing type ‘struct data *’ using type ‘struct data’
struct data *temp = vehicles[j];
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的数据结构

struct data{

    char *model;
    float engineSize;
    int cost;
    char *color;
};
Run Code Online (Sandbox Code Playgroud)

这是我目前为我正在使用的排序运行的代码

    void bubbleSortFloats(struct data vehicles[], int check)
    {
       int i, j, n;

       n = sizeof(vehicles)/sizeof(vehicles[0]);

      // If check == 1 then ascending sort
      if(check == 1){

       for (i = 0; i < n-1; i++){     

           // Last i elements are already in place   
           for (j = 0; j < n-i-1; j++){


               if (vehicles[j].engineSize > vehicles[j+1].engineSize){

                    struct data temp = vehicles[j];
                    vehicles[j] = vehicles[j+1];
                    vehicles[j+1] = temp; 

               }
            }
        }
      }
      // If check == 0 then decending sort
      if(check == 0){

        for (i = 0; i < n-1; i++){     

           // Last i elements are already in place   
           for (j = 0; j < n-i-1; j++){


               if (vehicles[j].engineSize < vehicles[j+1].engineSize){

                    struct data temp = vehicles[j+1];
                    vehicles[j+1] = vehicles[j];
                    vehicles[j] = temp; 

               }
            }
        }
      }

    }
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的 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;
    }
Run Code Online (Sandbox Code Playgroud)

Pab*_*blo 5

这段代码

sizeof(vehicles)/sizeof(vehicles[0]);
Run Code Online (Sandbox Code Playgroud)

仅适用于真正的数组。

void bubbleSortFloats(struct data vehicles[], int check);
Run Code Online (Sandbox Code Playgroud)

vehicles 看起来像一个数组但实际上是一个指针,这个函数定义和

void bubbleSortFloats(struct data *vehicles, int check);
Run Code Online (Sandbox Code Playgroud)

在 C 中,你不能将真正的数组传递给函数,它们总是作为指针传递。即使是这样的代码:

void foo(int arr[10])
{
    printf("%lu\n", sizeof arr / sizeof *arr);
}

int main()
{
    int arr[10] = { 0 };
    foo(arr);
}
Run Code Online (Sandbox Code Playgroud)

我收到编译器的警告:

sizeof(vehicles)/sizeof(vehicles[0]);
Run Code Online (Sandbox Code Playgroud)

如果我执行它,我得到 2,因为在我的系统上,指针的大小是 8,而 an 的大小int是 2。这就是为什么在sizeof arr / sizeof *arr传递数组的函数中不起作用。

您必须调用 之前计算数组的长度bubbleSortFloats并将该长度传递给函数。正确的功能是:

void bubbleSortFloats(struct data *vehicles, size_t len, int check)
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

我们看不到你是如何创建数组的,但是创建它的函数会知道大小:

void foo(void)
{
    struct data vehicles[SOME_LEN];
    ...

    bubbleSortFloats(vehicles, sizeof vehicles / sizeof *vehicles, check);
}
Run Code Online (Sandbox Code Playgroud)

或者如果你这样做了 malloc

void foo(void)
{
    size_t len = ...;

    struct data *vehicles = malloc(len * sizeof *vehicles);
    if(vehicles == NULL)
        return;

    ...

    bubbleSortFloats(vehicles, len, check);
}
Run Code Online (Sandbox Code Playgroud)

编辑

Jeffrey Hennen 在评论区问道

我说清楚了。我将要返回一个结构体和一个结构体数组大小的整数计数

就像我在评论中所说的那样,如果您想返回多个值,您基本上有两种选择:

  1. 声明一个封装所有返回值的结构体并返回
  2. 或者将指针传递给函数,让函数更新指针提供的值。

你选择了方式 1,所以我猜你有这个结构:

struct values {
    struct data *vehicles;
    size_t count;
};
Run Code Online (Sandbox Code Playgroud)

那么你返回它的方式是可以的。当然,您应该检查 lastmalloc没有返回NULL(尽管您在整个函数中都忽略了这一点)。

第二种选择是:

最简单的方法是:

struct data *readFile(size_t *length) {
    if(length == NULL)
        return NULL;

    ...
    while (getline(&line, &len, fp)!= -1){
        ...
    };

    // NOT NEEDED ANYMORE
    //struct values *value = malloc(sizeof(struct values));
    //value.vehicles = vehicles;
    //value.count = count;

    *length = count; // UPDATE the length through the pointer
    return vehicles; // return the array
}
Run Code Online (Sandbox Code Playgroud)

调用函数将执行以下操作:

void foo(void)
{
    size_t count;
    struct data *vehicles = readFile(&count);
    if(vehicles == NULL)
    {
        // error
        return;
    }

    do_something_with_the_vehicles(vehicles, count);

    free_the_vehicles(vehicles, count);
}
Run Code Online (Sandbox Code Playgroud)

当然,您必须编写一个函数free_the_vehicles(您当然可以选择其他名称)来释放所有分配的内存。