I am new to structs and pointer. Trying to learn them.Tried a simple sorting function with struct and pointer and but having some issues. Can some one help me understand them?
#include<stdio.h>
#include <stdlib.h>
int*
sorting (int* arg)
{
int temp=0,j,i;
for(i=1;i<5;i++)
{
for(j=0;j<5-i;j++)
{
if(arg[j] >arg[j+1])
{
temp=arg[j];
arg[j]=arg[j+1];
arg[j+1]=temp;
}
}
}
return arg;
}
int main(){
int i;
int n =5;
int *result_;
int *sorting_1_arg = malloc(n * sizeof *sorting_1_arg);
printf("Sort\n ");
printf("Enter 5 elements to sort: ");
for (i =0; i <n; i++){
scanf("%d", &sorting_1_arg[i]);
}
result_4 = sorting(sorting_1_arg);
printf ("Sorted List recieved from Server ");
for (i =0; i <n; i++){
printf("%d",sorting_1_arg[i]);
}
return 0;
}
When I run this code:
In function 'int main()':
[Error] invalid conversion from 'void*' to 'int*' [-fpermissive]
The programs seems fine to me. Except that there is an undeclared variable on line 39:
result_4 = sorting(sorting_1_arg);
Correct it to
result_ = sorting(sorting_1_arg);
The program runs fine. Just add a "\\n" for new lines to make it look better.
The site in here gives you a good introduction to structures and pointers.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.