gpt4 book ai didi

c - 查找结构体数组 C 的大小

转载 作者:行者123 更新时间:2023-11-30 14:50:51 24 4
gpt4 key购买 nike

我正在尝试查找用于对其进行排序的结构数组的大小。不知道如何使用通常在非结构数组中使用的 sizeof() 函数来实现它。

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

error: incompatible types when initializing type ‘struct data *’ using type ‘struct data’
struct data *temp = vehicles[j];

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

struct data{

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

这是我当前正在为我正在使用的类型运行的代码

    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;

}
}
}
}

}

这是我正在使用的 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;
}

最佳答案

这段代码

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

仅适用于true数组。

void bubbleSortFloats(struct data vehicles[], int check);

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

相同
void bubbleSortFloats(struct data *vehicles, int check);

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

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

int main()
{
int arr[10] = { 0 };
foo(arr);
}

我收到编译器的警告:

a.c: In function ‘foo’:
a.c:6:25: warning: ‘sizeof’ on array function parameter ‘arr’ will return size of ‘int *’ [-Wsizeof-array-argument]
printf("%lu\n", sizeof arr);
^~~
a.c:4:14: note: declared here
void foo(int arr[10])
^~~

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

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

void bubbleSortFloats(struct data *vehicles, size_t len, int check)
{
...
}

我们不知道你如何创建数组,但创建它的函数会知道尺寸:

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

bubbleSortFloats(vehicles, sizeof vehicles / sizeof *vehicles, check);
}

或者如果你使用malloc来实现

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

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

...

bubbleSortFloats(vehicles, len, check);
}
<小时/>

编辑

Jeffrey Hennen asked in the comment section:

I made it clear. I am going to want to return a structure and an int count for the size of the array of structures

就像我在评论中所说的,如果你想返回多个值,你可以本质上有两个选择:

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

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

struct values {
struct data *vehicles;
size_t count;
};

那么你返回它的方式就可以了。当然,您应该检查最后一个malloc没有返回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
}

调用函数会这样做:

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);
}

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

关于c - 查找结构体数组 C 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48727415/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com