spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Is there a way to program a better code for sorting a struct in C?


You could, and you probably should, use the standard sorting function qsort(3). You need to give it a compare function.

static int compare_clothes_by_price
(const void*p1, const void*p2) 
{
   const clothes* c1 = (const clothes*)p1;
   const clothes* c2 = (const clothes*)p2;
   if (c1->price == c2->price) return 0;
   else if (c1->price < c2-price) return -1;
   else if (c1->price > c2-price) return 1;
   // this is reached only if a price is NAN
   abort();
}

Then you call

 qsort(pt, count, sizeof(clothes),
compare_clothes_by_price);

Categories : C

Related to : Is there a way to program a better code for sorting a struct in C?
Const struct assigned to struct
It is not the same. You are trying to assign const pointer to a non const pointer. Another words: trying to assign Mutable pointer to a constant StudentType to a non-const pointer. It does make sense to change something that is declared const to no-cont, meaning you could change a value of a constant. What you are trying to do is to copy one object to another using shallow copy. in order to do so

Categories : Visual C++
C: Return even number in array program by using pointers, why does my program crush after printing the result
for(i = 0; i <= sizeof(test); i++){ printf("%d ", *test[i] ); } sizeof(test) isn't the number of elements in the array test, it's sizeof(int*) which is 8 (bytes), assuming you're on a 64-bit system. It's returning the size of a pointer to a memory address, not the actual length of the array. So your loop goes outside the bounds of the array which probably causes your problem. Al

Categories : C
Making a program with a range that can change during the course of the program (Java)
private static void RepeatedGuesses() { while (1 != UserAnswer) { UserAnswer = reader.nextInt(); if (2 == UserAnswer) { rFinder = (int) (generator.nextInt(100 - guess)); guess = rFinder + guess; System.out.print(guess + ": Is this your number? Enter Yes or enter Higher or enter Lower:"); } else if (3 == Use

Categories : Java
Use reflect to set values of struct of struct values
Here's the working program. Explanation below. package main import "fmt" import "strings" import "reflect" type Alias struct { Name string DevicePath string GuiPath string Setpoint string Command string Status string FunctionCmds string } type Manifold struct { Final01 Alias "Final01,/Gaspanel/Shared/Final01,,,wOpen,rIsOpen,"

Categories : Go
typedef is used on same struct twice
The first typedef gives the (anonymous) struct a typename. The second typedef defines a type of a pointer to the other. tour_struct tour; // declares a struct. tour_t ptr; // declares a pointer to a struct.

Categories : C
Recently Add
Rewrite of IDA decompiled function
Computing the average of grades in C
passing structures to functions
Use semaphores for handling sockets in C
How to search for a string pattern inside html, coding in C?
sprintf invalid format '%d'
Why can't this c program run correctly?
Low Pass filter in C
Child process does not print anything
C: datatypes. sqrt function working with int why?
How to implement Serial Port Profile Link Command used in ConnectBlue Bluetooth Module?
Rerunning cancelled pthread
How to print out a string that is pointed to by a char pointer?
Can unverified scanf call cause an undefined behavior?
nested structures in C
Portable Makevars for R package using C, GSL and OpenMP with help of Rcpp
program doesn't run(getting RUN FINISHED Segmentation fault core dumped)
Finding the sign of a Lua number, from C
Width of symbols created by gcc's objectcopy
Strange behavior when using free in c program
Can't open image C
Strange C code - dynamic arrays?
C program printing weird characters
Fault in decimal to binary in c
How to detect broken pipe on input?
How can I ivestigate use of the register keyword in C?
File scaning with fscanf
why gcc generates "leal -8240(%ebp), %eax; cmpl $1, %eax; ja XXX" instead of jmp instruction?
Wierd Problems With My Own Custom Written Word Search Algorithm
Trouble with making a shell in C
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.