C pass int array pointer as parameter into a function

前端 未结 9 1467
借酒劲吻你
借酒劲吻你 2020-12-08 04:39

I want to pass the B int array pointer into func function and be able to change it from there and then view the changes in main function

#include 

        
9条回答
  •  感情败类
    2020-12-08 05:07

    Using the really excellent example from Greggo, I got this to work as a bubble sort with passing an array as a pointer and doing a simple -1 manipulation.

    #include
    
    void sub_one(int (*arr)[7])
    {
         int i; 
         for(i=0;i<7;i++)
        {
            (*arr)[i] -= 1 ; // subtract 1 from each point
            printf("%i\n", (*arr)[i]);
    
        }
    
    }   
    
    int main()
    {
        int a[]= { 180, 185, 190, 175, 200, 180, 181};
        int pos, j, i;
        int n=7;
        int temp;
        for (pos =0; pos < 7; pos ++){
            printf("\nPosition=%i Value=%i", pos, a[pos]);
        }
        for(i=1;i<=n-1;i++){
            temp=a[i];
            j=i-1;
            while((temp=0)) // while selected # less than a[j] and not j isn't 0
            {
                a[j+1]=a[j];    //moves element forward
                j=j-1;
            }
             a[j+1]=temp;    //insert element in proper place
        }
    
        printf("\nSorted list is as follows:\n");
        for(i=0;i

    I need to read up on how to encapsulate pointers because that threw me off.

提交回复
热议问题