Hi everyone . I'm still a beginner in C especially with structures and pointers. So what i want to do here is to sort an Array of pointers to Structs based on a string inside the Struct . All the Element are dynamically allocated . And i want to know how to free the allocated Memory . My code is written in German so i will be writing only the parts that i don't understand in English . thank you in advance I appreciate your help. -
typedef struct Vehicle {
-
char *manufacturer;
-
char *serialnumber;
-
int weight;
-
};
-
void SortByID(struct fahrzeug** data, int Anzahl){
-
struct Vehicle *temp= (struct Vehicle*)malloc( sizeof(struct Vehicle) ) ;
-
int i =0 ;
-
while ( i < Anzahl && strcmp(data[i]->serialnumber, data[i+1]->serialnumber) < 0)
-
{
-
temp = data[i+1];
-
data[i + 1] = person[i];
-
data[i]=temp ;
-
i++;
-
}
-
}
-
free(temp);
-
temp=NULL;
-
}
-
int main(){
-
struct Vehicle **array = NULL ;
-
array=(struct Vehicle**)malloc( 5 * sizeof(struct Vehicle*) );
-
SortByID ( &array, count ) ;
-
return 0;
-
}
and i will be uploading the whole Programme but it is written in German and there is a lot of non complete function in it .
thank you for your Healp .I've already found a solution with you Help and the Help of Other Programmer on stackoverflow . I'm still a beginner in c that's why there is a lot that i have to learn. I appreciate your Help
this is a working version of what i am trying to do - #include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
-
struct Vehicle {
-
char *manufacturer;
-
char *serialnumber;
-
int weight;
-
};
-
-
int cmp_serialnumber(const void *p1, const void *p2)
-
{
-
const struct Vehicle *v1 = *(struct Vehicle * const *)p1;
-
const struct Vehicle *v2 = *(struct Vehicle * const *)p2;
-
-
return strcmp(v1->serialnumber, v2->serialnumber);
-
-
/*
-
// Alternatively You could write this function as this less readable one liner
-
return strcmp((*(struct Vehicle * const *)p1)->serialnumber,
-
(*(struct Vehicle * const *)p2)->serialnumber);
-
*/
-
}
-
-
int cmp_manufacturer(const void *p1, const void *p2)
-
{
-
const struct Vehicle *v1 = *(struct Vehicle * const *)p1;
-
const struct Vehicle *v2 = *(struct Vehicle * const *)p2;
-
-
return strcmp(v1->manufacturer, v2->manufacturer);
-
}
-
-
void print_vehicles(struct Vehicle **vehicles, int n) {
-
for (int i=0; i<n; i++) {
-
printf("%s, %s, %d\n", vehicles[i]->serialnumber,
-
vehicles[i]->manufacturer, vehicles[i]->weight);
-
}
-
}
-
-
-
#define N_VEHICLES 5
-
char *manufacturers[] = {"McLaren", "Ferrari", "Renault", "Mercedes", "Alfa Romeo"};
-
char *serialnumbers[] = {"SN500", "SN4", "SN8", "SN2", "SN1"};
-
-
-
int main(){
-
struct Vehicle **vehicles = NULL;
-
-
vehicles = (struct Vehicle**)malloc(N_VEHICLES * sizeof(struct Vehicle *));
-
-
for(int i=0; i<N_VEHICLES; i++) {
-
vehicles[i] = (struct Vehicle *)malloc(sizeof(struct Vehicle));
-
vehicles[i]->manufacturer = manufacturers[i];
-
vehicles[i]->serialnumber = serialnumbers[i];
-
vehicles[i]->weight = 1000;
-
}
-
-
printf("Before\n");
-
print_vehicles(vehicles, N_VEHICLES);
-
printf("\n");
-
-
// sort by serial number
-
qsort(vehicles, N_VEHICLES, sizeof(struct Vehicle *), cmp_serialnumber);
-
printf("Sorted by serial number\n");
-
print_vehicles(vehicles, N_VEHICLES);
-
printf("\n");
-
-
// sort by manufacturer
-
qsort(vehicles, N_VEHICLES, sizeof(struct Vehicle *), cmp_manufacturer);
-
printf("Sorted by manufacturer\n");
-
print_vehicles(vehicles, N_VEHICLES);
-
-
return 0;
-
}
5 5556
You might read this: https://bytes.com/topic/c/insights/7...rrays-revealed
You create each Vehicle using malloc.
You load data into each Vehicle.
You place the Vehicle* for these Vehicle in the array of Vehicle**
When you are finished with each Vehicle*, you free the pointer.
The sort just rearranges the Vehicle* in the array. It does not malloc.
If you have malloc data inside your struct you need to write your own free function that will free the member allocations and then free the struct itself.
Post again if this does not help.
thank you for replying
I've tried to sort the array of pointer just by swapping the pointers inside it . it will be easier . i 've used the qsort funtion but without success - int compare(const void *a, const void *b) {
-
struct Vehicle * const *one = a;
-
struct Vehicle * const *two = b;
-
-
return strcmp((*one)->serialnumber, (*two)->serialnumber);
-
}
-
void SortByID(struct Vehicle** data, int Anzahl){
-
qsort( data, Anzahl, sizeof(struct Vehicle*) ,compare );
-
}
Have you got a clean compile yet?
This code: - void fahrzeugSortByID(struct fahrzeug** daten, int Anzahl){
-
struct fahrzeug *temp= (struct fahrzeug*)malloc( sizeof(struct fahrzeug) ) ;
-
for(int i =0 ; i<Anzahl ; i++){
-
temp = &(daten[i]);
-
daten[i]=&(daten[i+1]);
-
daten[i+1] =&temp ;
-
}
has indirection errors. daten is a fahrzeug** so daten[i] is a fahrzeug*. Therefore, &daten[i] is a fahrzeug**. It is being assigned to temp which is a fahrzeug*. My compiler says no-no.
Also, this is C so you can't assign a struct using the assignment operator =. You have to write a function to make a memberwise copy of each element of the struct.
This code: temp = &(daten[i]) will destroy the contents of temp which is a pointer to memory allocated by malloc. This memory is now not able to be freed. This is, this is a leak.
thank you for your Healp .I've already found a solution with you Help and the Help of Other Programmer on stackoverflow . I'm still a beginner in c that's why there is a lot that i have to learn. I appreciate your Help
this is a working version of what i am trying to do - #include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
-
struct Vehicle {
-
char *manufacturer;
-
char *serialnumber;
-
int weight;
-
};
-
-
int cmp_serialnumber(const void *p1, const void *p2)
-
{
-
const struct Vehicle *v1 = *(struct Vehicle * const *)p1;
-
const struct Vehicle *v2 = *(struct Vehicle * const *)p2;
-
-
return strcmp(v1->serialnumber, v2->serialnumber);
-
-
/*
-
// Alternatively You could write this function as this less readable one liner
-
return strcmp((*(struct Vehicle * const *)p1)->serialnumber,
-
(*(struct Vehicle * const *)p2)->serialnumber);
-
*/
-
}
-
-
int cmp_manufacturer(const void *p1, const void *p2)
-
{
-
const struct Vehicle *v1 = *(struct Vehicle * const *)p1;
-
const struct Vehicle *v2 = *(struct Vehicle * const *)p2;
-
-
return strcmp(v1->manufacturer, v2->manufacturer);
-
}
-
-
void print_vehicles(struct Vehicle **vehicles, int n) {
-
for (int i=0; i<n; i++) {
-
printf("%s, %s, %d\n", vehicles[i]->serialnumber,
-
vehicles[i]->manufacturer, vehicles[i]->weight);
-
}
-
}
-
-
-
#define N_VEHICLES 5
-
char *manufacturers[] = {"McLaren", "Ferrari", "Renault", "Mercedes", "Alfa Romeo"};
-
char *serialnumbers[] = {"SN500", "SN4", "SN8", "SN2", "SN1"};
-
-
-
int main(){
-
struct Vehicle **vehicles = NULL;
-
-
vehicles = (struct Vehicle**)malloc(N_VEHICLES * sizeof(struct Vehicle *));
-
-
for(int i=0; i<N_VEHICLES; i++) {
-
vehicles[i] = (struct Vehicle *)malloc(sizeof(struct Vehicle));
-
vehicles[i]->manufacturer = manufacturers[i];
-
vehicles[i]->serialnumber = serialnumbers[i];
-
vehicles[i]->weight = 1000;
-
}
-
-
printf("Before\n");
-
print_vehicles(vehicles, N_VEHICLES);
-
printf("\n");
-
-
// sort by serial number
-
qsort(vehicles, N_VEHICLES, sizeof(struct Vehicle *), cmp_serialnumber);
-
printf("Sorted by serial number\n");
-
print_vehicles(vehicles, N_VEHICLES);
-
printf("\n");
-
-
// sort by manufacturer
-
qsort(vehicles, N_VEHICLES, sizeof(struct Vehicle *), cmp_manufacturer);
-
printf("Sorted by manufacturer\n");
-
print_vehicles(vehicles, N_VEHICLES);
-
-
return 0;
-
}
Thank you. you are the only one who helped me here .
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Geetesh |
last post by:
Recently i saw a code in which there was a structer defination similar
as bellow:
struct foo
{
int dummy1;
int dummy2;
int last
};
In application the above array is always allocated at...
|
by: beetle |
last post by:
Hello,
I'm storing data in several different binary tree's.
The root node is located in a struct containing general data about the
tree.
struct lnode {
char *fname;
int nentry;
|
by: hermes_917 |
last post by:
I want to use memcpy to copy the contents of one struct to another
which is a superset of the original struct (the second struct has extra
members at the end). I wrote a small program to test...
|
by: Jón Sveinsson |
last post by:
Hello everone
I have code from c which I need to convert to c#, the c
code is following, as you can see there is a struct
inside struct
typedef struct
{
some variables....
|
by: Jeff |
last post by:
Given a struct, TEST, I'd like to create a static end of array marker,
also of type test. This way static arrays of type TEST could be easily
created and their end could be marked with the end of...
|
by: Allie |
last post by:
How would I go about sorting this structure by title?
typedef struct {
char author;
char title;
char code;
int hold;
int loan;
} LIBRARY;
|
by: Daniel Rudy |
last post by:
Is the following code legal?
typedef stuct data_type_tag {
int var1;
int var2;
int var3;
struct {
void *p;
size_t size;
unsigned int flags;
|
by: Santosh Nayak |
last post by:
Hi,
Is it possible to sort the array of struct based on the data members.
e.g.
struct {
int a ;
float b ;
char c ;
} TEMP ;
|
by: Santosh Nayak |
last post by:
Hi,
Is it possible to sort the array of struct based on the data members.
e.g.
struct {
int a ;
float b ;
char c ;
} TEMP ;
|
by: arnuld |
last post by:
I am passing an array of struct to a function to print its value. First
I am getting Segfaults and weired values. 2nd, is there any elegant way to
do this ?
/* Learning how to use an array...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |