简体   繁体   English

使用结构和指针排序

[英]sort using struct and pointers

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. 尝试学习它们。尝试了一个简单的具有struct和pointer的排序功能,但是存在一些问题。 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: 除了第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. 只需为新行添加“ \\ n”,使其看起来更好。

The site in here gives you a good introduction to structures and pointers. 这里的站点为您很好地介绍了结构和指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM