scanf to struct doesnt work
Tag : c , By : Thaweesak Suksuwan
Date : March 29 2020, 07:55 AM
wish helps you I'm having this function and I need to get coordinates to a structure. These are the structures: scanf("%d %d",pf1->location.x_l,pf1->location.y_l);
scanf("%d %d",&(pf1->location.x_l), &(pf1->location.y_l));
|
Why qsort from stdlib doesnt work with double values? [C]
Date : March 29 2020, 07:55 AM
should help you out You want to sort doubles but you compare them as ints... Try this comparison function: int cmpfunc (const void * a, const void * b)
{
if (*(double*)a > *(double*)b)
return 1;
else if (*(double*)a < *(double*)b)
return -1;
else
return 0;
}
|
Retrieving struct from array of struct in C for qsort
Date : November 14 2020, 04:51 PM
I wish did fix the issue. The problem is that your typedef defines test as a pointer type, not a plain type. Then test* becomes a double-pointer, i.e. struct test**. When you write first->data1, you are applying -> operator to a pointer to pointer to struct test, which is not a pointer to a struct. Since test* is a double-pointer, you need to rewrite your comp to dereference it once before obtaining a member, as follows: int comp (const void *a, const void* b){
const test* first = (const test*)a;
const test* second = (const test*)b;
return (*first)->data1 - (*second)->data1;
}
|
qsort doesnt change my array order
Date : March 29 2020, 07:55 AM
Any of those help i have an array of structures (Employee): , You need to change this: qsort(&employeeArray, 2, sizeof(employee_t), compareEmployeesBySalary);
qsort(employeeArray, 2, sizeof(employee_t *), compareEmployeesBySalary);
int compareEmployeesBySalary(const void* a, const void* b){
employee_t* one = *(employee_t **)a;
employee_t* two = *(employee_t **)b;
if (one->salary == two->salary)
return 0;
else if (one->salary > two->salary)
return 1;
else
return -1;
}
|
Scanf in struct containing 'double' array doesnt work?
Date : March 29 2020, 07:55 AM
should help you out The struct members x and y arrays can hold only one element each. But you are reading 2 elements as input. In C, array index ranges from 0 to N-1. Your code has undefined behaviour due to out of bounds access. struct _point2d
{
double x[2]
double y[2];
};
|