I was learning about structs and heard that structs allow you to copy everything by just using =
, so I was trying to sort a struct using this property combined with pointers, however there is something wrong with the code obviously that I do not understand, what should I do instead because this just copies the struct pointed to at by the pointer at the 0th index,and puts that to every element of the struct array.
Printing the sorted array just throws garbage values, I haven't been able to grasp pointers to array completely, if anyone could provide with a link to some resource, it would be great.
Struct body:
struct bankacc{
char name[20];
int accno;
float balance;
};
function for swap:
void bsortDesc(struct bankacc arr[], int n)
{
//int i, j;
//struct bankacc *temp=NULL;
//struct bankacc *ptr=arr;
struct bankacc *temp=NULL;
struct bankacc *ptr=arr;
for(int i=0;i<n;i =1){
for(;ptr<(arr n);ptr =1)
{
temp=ptr;
(ptr)=(ptr 1);
(ptr 1)=temp;
printf("temp name %s \n",temp->name);
printf("temp accno %d \n",temp->accno);
printf("temp name %f \n",temp->balance);
printf("\n_____________\n");
}
}
}
I also tried making a temp variable and perform a value swap, but it doesn't work either i. e.
temp=*ptr;
ptr=(ptr 1);
*(ptr 1)=temp;
Kindly assist me
CodePudding user response:
To swap two objects of a structure type using pointers to the objects you can write something like the following
struct bankacc temp = *ptr;
*ptr = *( ptr 1 );
*( ptr 1 ) = temp;
Pay attention to that in this for loop
for(;ptr<(arr n);ptr =1)
there can be an access to memory beyond the array when ptr
is equal to arr n - 1
due to the expression ptr 1
.
CodePudding user response:
I see you talking about sorting, but not actually sorting.
If you need to sort an array of structs, you can use qsort
that will take care of all of the swapping for you.
You should start by writing a sort function (or multiple), such as:
int compare_name(const void *p1, const void *p2)
{
const struct bankacc *acc1 = p1;
const struct bankacc *acc2 = p2;
return strcmp(acc1->name, acc2->name);
}
int compare_accno(const void *p1, const void *p2)
{
const struct bankacc *acc1 = p1;
const struct bankacc *acc2 = p2;
return acc1->accno - acc2->accno;
}
Then you could just sort using qsort
:
qsort(arr, n, sizeof *arr, compare_accno);
It is a good exercise to do the swap of two indexes yourself. For that you could use:
void swap(struct bankacc *acc1, struct bankacc *acc2)
{
struct bankacc tmp;
tmp = *acc1;
*acc1 = *acc2;
*acc2 = tmp;
}
And then call the function as required, e.g.:
swap(&arr[3], &arr[8]);
CodePudding user response:
In your code, ptr is a pointer. *ptr is a struct.
So here is what your example code actually does:
- It copies the struct *ptr to temp.
- It increases the pointer ptr by 1.
- It copies the struct from tmp to *(ptr 1).
Since you increased ptr by 1 before step 3, what was at *ptr has been moved to *(original ptr 2). Step 2 should have been *ptr = *(ptr 1).
It would probably be better not to use pointers at all. Instead of a pointer ptr, use an index size_t j, and assign for example tmp = arr[j]; arr[j]= arr[j 1]; arr[j 1] = tmp; Much less confusing.