Sorting arrays of strings/struct in C - c

I'm a newbie with C language and I need to make a function to sort an array of struct Student data types (after the element Student.ime alphabetically). I am not really sure where to put the pointers so I can return the new array.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student {
int id;
char ime[20];
char prezime[20];
char brindexa[20];
struct Datum datum_rodjenja;
};
struct Student sortiraj(struct Student niz[], int vel)
{
int i, j;
struct Student tempo;
tempo = niz[0];
for(j=0 ; j<vel ; j++)
{
for(i=j ; i<vel ; i++)
{
if(strcmp(tempo.ime,niz[i].ime)>0)
{
tempo = niz[i];
}
i++;
}
niz[j] = tempo;
j++;
}
return niz;
}
The array is stored in a .txt file but that is not the problem. One more thing, how do I call the function in the main(). I thought maybe like this?
niz=sortiraj(niz, vel);
Can someone give me any tips please. Thank you.

You seem to be sorting the array in-place, so you don't really need to return it at all. Also, use the standard library function qsort():
int cmp(const void *ap, const void *bp)
{
const struct Student *a = ap, *b = bp;
return strcmp(a->ime, b->ime);
}
struct Student students[] = { /* whatever */ };
qsort(
students,
sizeof(students) / sizeof(studends[0]),
sizeof(students[0]),
cmp
);
Also, please use English function and variable names.

Have you considered using the qsort() function? Example:
If you have an array of strings say strings, (Note: not an array of structs), with the number of strings being say, cnt then:
qsort(strings, cnt, sizeof(char*), sortstring);
With the function sortstring defined as:
static int sortstring( const void *str1, const void *str2 )
{
const char *rec1 = *(const char**)str1;
const char *rec2 = *(const char**)str2;
int val = strcmp(rec1, rec2);
return val;
}
//for a simple test, run this main with the code above:
int main(void)
{
char *strings[]={"this", "is", "a", "test", "of", "the", "qsort", "function", "to", "try"};
int strlen = sizeof(strings)/sizeof(char *);
qsort(strings, strlen, sizeof(char *), sortstring);
return 0;
}
//Note my environment required #include <ansi_c.h>

First of all, your function signature is not marked return type as an Array of Students. Second of all, I don't know which sorting algorithm you're trying to use, but your implementation isn't right.
If you correct your function signature, you shouldn't get any error by the way you're calling the:
struct Student* sortiraj(struct Student niz[], int vel)
tips about your sortiraj: Check either selection sort or bubble sort for your sort algorithm. And for further study, you can check some recursive algorithms like merge sort and quick sort which are more advance and you need more programming knowledge to implement them.

Change the return type of function sortiraj to void.
Create and fill the struct Student niz[] array in function main.
From function main, pass the array and its length (vel) to function sortiraj.
And of course, no need to return anything from function sortiraj (as implied in section 1).

Since you are passing in a pointer, namely niz, you are changing the memory that niz points to. This means that you don't have to return niz.
However if you want to return niz your function's return type must be the same type as niz. Currently you have a return type of just struct Student so you should be getting a compilation error.

Related

What am I doing wrong in passing a struct around in C?

So I am working on a project in C that requires that I pass pointers to a struct into functions. The project is structured as follows:
struct structName {
unsigned short thing2;
char thing1[];
};
void function_1(struct structName *s) {
strcpy(s->thing1, "Hello");
printf("Function 1\n%s\n\n", s->thing1); // prints correctly
}
void function_2(struct structName *s) {
// can read thing2's value correctly
// thing1 comes out as a series of arbitrary characters
// I'm guessing it's an address being cast to a string or something?
printf("Function 2\n%s\n\n", s->thing1); // prints arbitrary characters ('É·/¨')
}
int main() {
struct structName s;
function_1(&s);
printf("Main\n%s\n\n", s.thing1);
function_2(&s);
printf("Main 2\n%s\n\n", s.thing1);
}
This code outputs the following:
Function 1
Hello
Main
Hello
Function 2
É·/¨
Main 2
É·/¨
Obviously, the program has more than just what I've written here; this is just a simplified version; so if there's anything I should check that might be causing this let me know. In all honesty I reckon it's probably just a stupid rookie error I'm making somewhere.
[EDIT: Seems like s.thing1 is being mutated in some way in the call to function_2(), since the odd value is replicated in main() - I should point out that in my program the printf()s are located right before the function call and in the first line of the function, so there's no chance that it's being written to by anything I'm doing. I've updated the example code above to show this.]
Thanks in advance!
The structure contains a flexible member at its end, if you declare a static object with this type, the length of this member will be zero, so strcpy(s->thing1, "Hello"); will have undefined behavior.
You are supposed to allocate instances of this type of structure with enough extra space to handle whatever data you wish to store into the flexible array.
Here is an example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct pstring {
size_t length;
char data[];
} pstring;
pstring *allocate_pstring(const char *s) {
size_t length = strlen(s);
pstring *p = malloc(sizeof(*p) + length + 1);
if (p != NULL) {
p->length = length;
strcpy(p->data, s);
}
return p;
}
void free_pstring(pstring *p) {
free(p);
}
int main() {
pstring *p = allocate_pstring("Hello");
printf("Main\n%.*s\n\n", (int)p->length, p->data);
free_pstring(p);
return 0;
}

access struct member with a variable or pointer in C

I have a struct and want to sort an array of the struct using merge sort.
I need to pass the sorting parameter to the merge function and access the struct members.
Can this be done in C?
example:
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;
};
in the main function, I have:
if (argc > 2) {
column_sort = argv[2];
}
now I want to call merge_sort(<array of struct>, <column_sort *>)
can I access the member in the array as array[0]->column_sort to compare?
now I want to call merge sort and pass the
I want to pass the array and the sorting parameter (that I need the array sorted by) can I use a variable in place of a members name ie..
arr[1].column_sort
instead of
arr[1].color
Sounds like you want to take a command line argument specifying the name of the field to sort on and then sort on that field.
For that, try code like:
#include <stdlib.h>
#include <string.h>
/* compare function for field 3: "num_critic_for_reviews" */
int compField3(const void *a, const void *b)
{
struct movie_imdb_data* aStruct = (struct movie_imdb_data*)a;
struct movie_imdb_data* bStruct = (struct movie_imdb_data*)b;
return (aStruct->num_critic_for_reviews < bStruct->num_critic_for_reviews)?
-1: (aStruct->num_critic_for_reviews > bStruct->num_critic_for_reviews)?
+1: 0;
}
/* also define other compare functions for each field */
int main()
{
const char* columnName = argv[2];
struct movie_imdb_data* parray;
parray = your-array;
int (*comp)(const void *, const void *, void *) = 0;
/* map the column name to compare function for that column */
if (strcmp(columnName, "num_critic_for_reviews") == 0)
{
comp = compField3;
}
/* map other names to compare function for column */
else if (...) {...}
else { exit(1); /* if not recognized column name */ }
qsort(parray, numElementsOfArray, sizeof(struct movie_imdb_data), comp);
...
}
Hope this helps!
There are different approaches to your problem:
you can write separate functions to compare structures on specific fields. In main, you would select the appropriate comparison function by testing the name of the field (or possibly generic names that are not field names). You would then pass this comparison function to mergesort (or qsort...).
if all members have the same type, you could determine the offset of the field from the beginning of the structure with macros offsetof(type, member). There is no generic way to compute these offsets, you need to write a series of tests or use a table. The comparison function would use casts to access the members:
size_t member_offset = offsetof(struct movie_imdb_data, duration);
int comp_int_member(const void *a1, const void *a2) {
const int *p1 = (const int *)((const unsigned char*)a1 + member_offset);
const int *p2 = (const int *)((const unsigned char*)a2 + member_offset);
return (*p1 > *p2) - (*p1 < *p2);
}
The downside of this latter approach is it can only handle fields with a given type.

How to parse a structure array as a parameter?

How should I parse an array of structures as parameter to a function?
For example, I have the following structure definition:
struct Town
{
char *TownName;
char **GiftList;
int *GiftCount;
int GiftTypes;
};
and a declaration of an array of such structures, in my main:
struct Town TownList[100];
struct Town AuxiliaryStructure;
I have written a custom sorting function for this array, in which I want to make use of each structure's fields, but I do not know how to provide the array TownList[100] to the sort function.
To pass an array of anything to a function, you can just pass a pointer to the first element plus an array length:
struct mystruct {
char* something;
/* ... */
}
struct mystruct myarray[100];
void do_something(struct mystruct* array, int length)
{
int i;
for (i=0; i<length; ++i)
{
array[i].something = ...
}
}
int main(void)
{
do_something(myarray, 100);
return 0;
}
Based on what you wrote, you would reference it in the caller and pass the result. For example:
void sort(char* str){
...
}
int main(){
for(int i=0; i<100; ++i){
sort(array[i].something);
}
}
Alternately you may want to pass the full array and handle it in another function, which would be like this:
void sort(struct mystruct * array){
...
}
int main(){
for(int i=0; i<100; ++i){
sort(array);
}
}
If, on the other hand, you meant that you want to sort the outer array of structures by the contents of something, then you would either have to pass the array as above and implement your own sort, or use a sorting function that takes a function pointer so you can write a comparator. The latter is available in the standard C library, and could be used something like this.
#include <stdlib.h>
int compare_mystruct_by_something(const void *a, const void *b){
return strcmp(((struct mystruct*)a)->something, ((struct mystruct *)b)->something);
}
int main(){
qsort(array, 100, sizeof(struct mystruct), compare_mystruct_by_something);
}

Sending array of structures to function

in order to complete a larger project, im trying to get an idea of how to send an array of structures, and a token of char* type to a function. my Pupose of this code is to do the following:
open file
tokenize file
send token,and array of structures to search function
search function will go through the arrayofstructures, using strcmp to find a match with token
if match found return 1, the main function will check for 1 or 0
if 0, dont add token to array of structures,if 1 add token to arrayof structures
i just wrote a small program to see if i could send the array,and token to a function and compare but i get so many errors im lost at what to do since i dont understand most of the errors.
#include <stdio.h>
#include <string.h>
int search(struct id array[],char* tok);
struct id
{
char name[20];
int age;
};
int main(void)
{
struct id person[2] = { {"John Smith", 25},
{"Mary Jones", 32} };
char* token = "Mary Jones"; /*char* for strtok() return type*/
search(person,token);
}
int search(struct id array[],char* tok)
{
int i,value;int size = 2;
for(i=0;i<size;i++)
{
if(strcmp(array[i].name,tok) == 0)
value = 0;
else
value = 1;
}
return value;
}
Place
int search(struct id array[],char* tok);
after struct declaration. And assign the return value from search to an int variable.
int found = search(person,token);
if(found == 0)
printf("Name is found\n"); // or whatever you want
Here is the code that you should use.
#include <stdio.h>
#include <string.h>
struct id
{
char name[20];
int age;
};
int search( const struct id array[], size_t n, const char *tok );
int main( void )
{
struct id person[2] = { {"John Smith", 25},
{"Mary Jones", 32} };
char* token = "Mary Jones"; /*char* for strtok() return type*/
printf( "%d\n", search( person, sizeof( person ) / sizeof( *person ), token ) );
return 0;
}
int search( const struct id array[], size_t n, const char *tok )
{
size_t i = 0;
while ( i < n && strcmp( array[i].name, tok ) != 0 ) ++i;
return n != 0 && i != n;
}
EDIT: I removed some typos.
The output is
1
that is the name has been found.
Take into account that the correct function search has to return 1 if the name is found.:)
Always declare structs / enums / unions before defining pointers to them, or using them in a function declaration, like this:
struct id;
Extra tip, introduce a typedef-name with the same id as the tag name at the same time:
typedef struct id id;
Always declare functions before first use, like this:
int search(struct id array[],char* tok);
Always define structs / enums / unions before using them for anything but what the first rule covers, like this:
struct id {
char name[20];
int age;
};
With typedef-name:
typedef struct id { /**/ } id;
Now, where possible, put the definition where you would otherwise need to put a forward-declaration.
Only exception: If you put the declaration in a header-file, that's fine.
That reduces superfluous redundancy.
Some more observations:
Don't use fixed-size fields for names and such. They are always too short.
Never modify string literals, failing to heed that prohibition results in Undefined Behavior, your program just became meaningless. Work with a copy instead.
/* Most implementations supply this non-standard function */
char* strdup(const char* s) {
size_t n = strlen(s)+1;
char* p = malloc(n);
if(p) memcpy(p, s, n);
return p;
}
When you pass an array, you are actually only passing a pointer to its first element, so pass an element-count too.
Types size_t or ssize_t are designed for that chore.
If you have an array named a, you get the element count using sizeof a / sizeof *a. Be sure that's not a pointer though!
Early return are good: Return success as early as possible.
Then you don't chance to forget your success in the next loop iteration (as happened to you), beside being faster.

Sorting structs in C with qsort

I have a struct in C like this:
typedef struct proces {
char ime[60];
char pid[60];
char ppid[60];
char stanje;
int stdat;
char niti[60];
char poraba[60];
} proces ;
I create about 100 of them and put them into an array
proces** procesi = malloc(sizeof(proces));
int x;
for(x=0; x<st; x++){
procesi[x] = (struct proces*)malloc(sizeof(proces));
}
Now I would like to sort them with qsort. But the qsort sorts it wrong.
The function looks like this:
int compar_ppid(const void *v1, const void *v2){
const proces *p1 = (proces*)v1;
const proces *p2 = (proces*)v2;
return(strcmp(p1->ppid, p2->ppid));
}
I checked the values that the compar_ppid is comparing and they seem to be something like �#d, when they should be numbers.
I guess I'm accessing the pointer and not the value, but I cant figure out what to change to get the right values.
Qsort call:
qsort(procesi, st, sizeof(proces*), compar_name);
The array you sort contains pointers to process, so your compare function should look like:
int compar_ppid(const void * v1, const void * v2)
{
process *const*p1 = v1, *const*p2 = v2;
return strcmp((*p1)->ppid, (*p2)->ppid);
}
and as BLUEPIXY points out the allocation of the array is not using the pointer size, but the struct size.

Resources