C pass int array pointer as parameter into a function

189,257

Solution 1

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].

Solution 2

Maybe you were trying to do this?

#include <stdio.h>

int func(int * B){

    /* B + OFFSET = 5 () You are pointing to the same region as B[OFFSET] */
    *(B + 2) = 5;
}

int main(void) {

    int B[10];

    func(B);

    /* Let's say you edited only 2 and you want to show it. */
    printf("b[0] = %d\n\n", B[2]);

    return 0;
}

Solution 3

If you actually want to pass an array pointer, it's

#include <stdio.h>

void func(int (*B)[10]){   // ptr to array of 10 ints.
        (*B)[0] = 5;   // note, *B[0] means *(B[0])
         //B[0][0] = 5;  // same, but could be misleading here; see below.
}

int main(void){

        int B[10] = {0};   // not NULL, which is for pointers.
        printf("b[0] = %d\n\n", B[0]);
        func(&B);            // &B is ptr to arry of 10 ints.
        printf("b[0] = %d\n\n", B[0]);

        return 0;
}

But as mentioned in other answers, it's not that common to do this. Usually a pointer-to-array is passed only when you want to pass a 2d array, where it suddenly looks a lot clearer, as below. A 2D array is actually passed as a pointer to its first row.

void func( int B[5][10] )  // this func is actually the same as the one above! 
{
         B[0][0] = 5;
}

int main(void){
    int Ar2D[5][10];
    func(Ar2D);   // same as func( &Ar2D[0] )
}

The parameter of func may be declared as int B[5][10], int B[][10], int (*B)[10], all are equivalent as parameter types.

Addendum: you can return a pointer-to-array from a function, but the syntax to declare the function is very awkward, the [10] part of the type has to go after the parameter list:

int MyArr[5][10];
int MyRow[10];

int (*select_myarr_row( int i ))[10] { // yes, really
   return (i>=0 && i<5)? &MyArr[i] : &MyRow;
}

This is usually done as below, to avoid eyestrain:

typedef int (*pa10int)[10];

pa10int select_myarr_row( int i ) {
   return (i>=0 && i<5)? &MyArr[i] : &MyRow;
}

Solution 4

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.

Solution 5

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;
}
Share:
189,257
stergosz
Author by

stergosz

Joomla &amp; WordPress developer based in Greece Developing Joomla extensions on tassos.gr Developing WordPress plugins on fireplugins.com

Updated on June 02, 2020

Comments

  • stergosz
    stergosz almost 4 years

    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 <stdio.h>
    
    int func(int *B[10]){
    
    }
    
    int main(void){
    
        int *B[10];
    
        func(&B);
    
        return 0;
    }
    

    the above code gives me some errors:

    In function 'main':|
    warning: passing argument 1 of 'func' from incompatible pointer type [enabled by default]|
    note: expected 'int **' but argument is of type 'int * (*)[10]'|
    

    EDIT: new code:

    #include <stdio.h>
    
    int func(int *B){
        *B[0] = 5;
    }
    
    int main(void){
    
        int B[10] = {NULL};
        printf("b[0] = %d\n\n", B[0]);
        func(B);
        printf("b[0] = %d\n\n", B[0]);
    
        return 0;
    }
    

    now i get these errors:

    ||In function 'func':|
    |4|error: invalid type argument of unary '*' (have 'int')|
    ||In function 'main':|
    |9|warning: initialization makes integer from pointer without a cast [enabled by default]|
    |9|warning: (near initialization for 'B[0]') [enabled by default]|
    ||=== Build finished: 1 errors, 2 warnings ===|
    
  • stergosz
    stergosz over 11 years
    if i try to edit B[2] in func i get some errors... code: *B[2] = 5;
  • Ed S.
    Ed S. over 11 years
    @fxuser: "I get some errors" is not a useful problem description. Ask for help as if you were the person being asked. You are getting an error because B is a pointer to int and B[2] returns an int, not a pointer. So, you just want B[2] = 5;
  • greggo
    greggo over 10 years
    And I note that C++ is getting templateable typedefs, so it should be possible to write ptr_to_arr_of<int,10> select_myarr_row ... .
  • Iulian Onofrei
    Iulian Onofrei over 7 years
    This is the answer I was looking for. Why would you do array + 2 instead of array[2]?