C pass int array pointer as parameter into a function

前端 未结 9 1418
借酒劲吻你
借酒劲吻你 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<stdio.h>
    
    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<a[j])&&(j>=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<n;i++)
        {
            printf("%d\n",a[i]);
        }
        printf("\nmedian = %d\n", a[3]);
        sub_one(&a);
    
        return 0;
    }
    

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

    0 讨论(0)
  • 2020-12-08 05:18

    In new code assignment should be,

    B[0] = 5
    

    In func(B), you are just passing address of the pointer which is pointing to array B. You can do change in func() as B[i] or *(B + i). Where i is the index of the array.

    In the first code the declaration says,

    int *B[10]
    

    says that B is an array of 10 elements, each element of which is a pointer to a int. That is, B[i] is a int pointer and *B[i] is the integer it points to the first integer of the i-th saved text line.

    0 讨论(0)
  • 2020-12-08 05:18

    The argument of func is accepting double-pointer variable. Hope this helps...

    #include <stdio.h>
    
    int func(int **B){
    
    }
    
    int main(void){
    
        int *B[10];
    
        func(B);
    
        return 0;
    }
    
    0 讨论(0)
  • 2020-12-08 05:18

    Make use of *(B) instead of *B[0]. Here, *(B+i) implies B[i] and *(B) implies B[0], that is
    *(B+0)=*(B)=B[0].

    #include <stdio.h>
    
    int func(int *B){
        *B = 5;     
        // if you want to modify ith index element in the array just do *(B+i)=<value>
    }
    
    int main(void){
    
        int B[10] = {};
        printf("b[0] = %d\n\n", B[0]);
        func(B);
        printf("b[0] = %d\n\n", B[0]);
        return 0;
    }
    
    0 讨论(0)
  • 2020-12-08 05:23

    In your new code,

    int func(int *B){
        *B[0] = 5;
    }
    

    B is a pointer to int, thus B[0] is an int, and you can't dereference an int. Just remove the *,

    int func(int *B){
        B[0] = 5;
    }
    

    and it works.

    In the initialisation

    int B[10] = {NULL};
    

    you are initialising anint with a void* (NULL). Since there is a valid conversion from void* to int, that works, but it is not quite kosher, because the conversion is implementation defined, and usually indicates a mistake by the programmer, hence the compiler warns about it.

    int B[10] = {0};
    

    is the proper way to 0-initialise an int[10].

    0 讨论(0)
  • 2020-12-08 05:23

    In the function declaration you have to type as

    VOID FUN(INT *a[]);
    /*HERE YOU CAN TAKE ANY FUNCTION RETURN TYPE HERE I CAN TAKE VOID AS THE FUNCTION RETURN  TYPE FOR THE FUNCTION FUN*/
    //IN THE FUNCTION HEADER WE CAN WRITE AS FOLLOWS
    void fun(int *a[])
    //in the function body we can use as
    a[i]=var
    
    0 讨论(0)
提交回复
热议问题