在排序的C函数数组 [英] Sort an array in a C function

查看:101
本文介绍了在排序的C函数数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我需要通过数组功能排序,并将它排序它,它的工作原理之外,但不能让它的功能内工作。需要在指针只是不知道通过怎样。

 的#include<&stdio.h中GT;无效的排序为(int *号,INT N){     / *排序给定的序列号,长度为n *的/    INT TEMP = 0,J,I;
        对于(i = 1; I< N;我++)
        {
            为(J = 0; J< N-I; J ++)
            {
                如果(数[J]≥数[J + 1])
                {
                    TEMP =号[J]。
                    一些研究[J] =号[J + 1];
                    一些研究[J + 1] =温度;
                }
            }
        }
    }诠释主(){    INT N = 20;
    为int * PTR =的malloc(N * sizeof的* PTR);
    INT I = 0;    而(I< N)
    {
         PTR [I] =兰特()%100;
         我++;
    }
    INT J = 0;
    而(J< N)
    {
        的printf(%d个,PTR [J]);
        J ++;
    }
    无效的排序(PTR,N);
    的printf(\\ n);    INT X = 0;
    而(X< N)
    {
        的printf(%d个,PTR [X]);
        X ++;
    }
}


解决方案

无效排序(PTR,N); 应该仅仅是排序(PTR ,N);

我可能是错的,但我相信,在C编译器,第一种形式是PTED作为一个函数原型,第二实际函数调用间$ P $。

So I need to pass the array to the function sort and have it sort it, it works outside but cant get it to work inside the function. Need to pass in the pointers just not sure how.

#include <stdio.h>

void sort(int* number, int n){

     /*Sort the given array number , of length n*/     

    int temp=0,j,i;
        for(i=1;i<n;i++)
        {
            for(j=0;j<n-i;j++)
            {
                if(number[j] >number[j+1])
                {
                    temp=number[j];
                    number[j]=number[j+1];
                    number[j+1]=temp;
                }
            }
        }
    }

int main(){

    int n = 20;
    int *ptr = malloc(n * sizeof *ptr);
    int i = 0;

    while (i< n)
    {
         ptr[i]=rand() %100;
         i++;
    }
    int j = 0;
    while (j < n)
    {
        printf("%d , ", ptr[j]);
        j++;
    }
    void sort(ptr, n);
    printf("\n");

    int x = 0;
    while (x < n)
    {
        printf("%d , ", ptr[x]);
        x++;
    }
}

解决方案

void sort(ptr, n); should just be sort(ptr, n);

I could be wrong on this, but I believe in a C compiler, the first form is interpreted as a function prototype and the second an actual function call.

这篇关于在排序的C函数数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆