[英]Sorting an Array of Struct with type Char, qsort
I am running into a zsh: segmentation fault (this error has to do with memory) I have no idea as to why it is occurring.我遇到了 zsh: 分段错误(此错误与内存有关)我不知道它为什么会发生。 I am trying to make a compare function for the string in my struct.我正在尝试为结构中的字符串创建一个比较函数。 I need the program to be able to sort through the strings in ascending order.我需要该程序能够按升序对字符串进行排序。
Current Code:当前代码:
#include <stdio.h>
#include <string.h>
#include "stdlib.h"
# define MAX 7
typedef struct{
double price;
char title[60];
} game;
game gArr[MAX];
game buildGame(double num, const char *title);
void printGame(game);
int compGamesByTitle(const void * a,const void * b);
int main(){
gArr[0] = buildGame(0.01, "Minecraft");
gArr[1] = buildGame(22.79 , "Opus Magnum");
gArr[2] = buildGame(7.79 , "TIS-100");
gArr[3] = buildGame(14.99 , "Trainz");
gArr[4] = buildGame(0 , "Code Combat");
gArr[5] = buildGame(7.79, "Lemmings Revolution");
gArr[6] = buildGame(64.96 , "Warcraft");
qsort(gArr,sizeof(gArr)/sizeof(gArr[0]), sizeof(gArr[0]), compGamesByTitle);
printf("Sorted Games:\n");
for (int i = 0; i < MAX; i++){
printGame(gArr[i]);
}
return 0;
}
int compGamesByTitle(const void *a,const void *b){
char **aa = (char **)a;
char **bb = (char **)b;
return strcmp(*aa,*bb);
}
game buildGame(double num, const char *title){
game g;
g.price = num; strcpy(g.title, title);
return g;
}
void printGame(game g){
printf("Game g: %.2f, %s\n", g.price, g.title);
}
I think the problem has to do with un-allocated memory but I'm not sure where or why.我认为问题与未分配的内存有关,但我不确定在哪里或为什么。
Required Output: Strings in the array of structs should be sorted alphabetically (az) in ascending order.必需的输出:结构数组中的字符串应按字母顺序 (az) 升序排序。
No, your problem is not related to uninitialized memory.不,您的问题与未初始化的内存无关。 The comparison function passed as the fourth argument of qsort
receives pointers to the array elements that are being compared.作为qsort
的第四个参数传递的比较函数接收指向正在比较的数组元素的指针。 Since your gArr
is an array of game
object, but you are casting to char**
inside the compGamesByTitle
you end up calling strcmp
on pointers that do not point at null-terminated byte strings, invoking undefined behavior .由于您的gArr
是一个game
对象数组,但是您在compGamesByTitle
中转换为char**
, compGamesByTitle
您最终会在不指向空终止字节字符串的指针上调用strcmp
,从而调用未定义的行为。
Your comparison function should really look like this你的比较函数应该是这样的
int compGamesByTitle(const void *a,const void *b){
const game *aa = a;
const game *bb = b;
return strcmp(aa->title, bb->title);
}
This is not an answer to the question but just som additional input to @UnholySheep answer.这不是问题的答案,而只是对@UnholySheep 答案的一些额外输入。
The这
game buildGame(double num, const char *title){
game g;
g.price = num; strcpy(g.title, title);
return g;
}
Should be more robust again long title names.长标题名称应该更健壮。 Suggesting建议
game buildGame(double num, const char *title) {
game g;
g.price = num;
g.title[0] = '\0';
strncat(g.title, title, sizeof(g.title) - 1U);
return g;
}
as long as game::title remains an array.只要 game::title 仍然是一个数组。
The use of strncat
over strncpy
is motivated by not writing more characters than necessary.在strncpy
使用strncat
动机是不编写不必要的字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.