【发布时间】:2018-03-06 22:49:07
【问题描述】:
我有一个结构,并想使用归并排序对该结构的数组进行排序。 我需要将排序参数传递给合并函数并访问结构成员。 这可以在 C 中完成吗?
示例:
struct movie_imdb_data {
char color[15];
char director_name[100];
int num_critic_for_reviews;
int duration; /// in min not date and time
int director_facebook_likes;
int actor_3_facebook_likes;
char actor_2_name[100];
int actor_1_facebook_likes;
int gross;
};
在main 函数中,我有:
if (argc > 2) {
column_sort = argv[2];
}
现在我想打电话给merge_sort(<array of struct>, <column_sort *>)
我可以以array[0]->column_sort 的身份访问数组中的成员进行比较吗?
现在我想调用归并排序并通过
我想传递数组和排序参数(我需要对数组进行排序)我可以使用变量代替成员名称,即..
arr[1].column_sort
而不是
arr[1].color
【问题讨论】:
-
now i want to call merge sort and pass the通过,什么? -
*argv[]传递的“排序参数”是什么意思? -
也许您需要结构中给定成员的偏移量? C 的
offsetof()宏是stddef.h中的ANSI C 库功能。它计算结构或联合类型中给定成员的偏移量(以字节为单位),作为 size_t 类型的表达式。 -
我想传递数组和排序参数(我需要排序的数组)我可以使用变量代替成员名称,即.. arr[1].column_sort 而不是 arr [1].颜色