34

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
  • 5,754
  • 13
  • 62
  • 133
  • Well, the error message explains it. You pass a pointer to an array of 10 `int *`, but `func` expects an `int**` (which is expected to be a pointer to the first element of an array of (10, presumably) `int*`s). How to fix it depends on what `func` does. – Daniel Fischer Dec 05 '12 at 19:22
  • func will simply edit B values like B[0], B[1] etc.. – stergosz Dec 05 '12 at 19:24
  • Then you probably want to pass just `B`. Since `B` is actually an array, passing `&B` is typically not useful, since `B` can't be changed (but its contents can be changed, and that's what you want to do). – Daniel Fischer Dec 05 '12 at 19:26
  • updated question with new code with errors when i try to edit an value of B in func function – stergosz Dec 05 '12 at 19:29
  • The answer to your original question is that `func` should be declared like this: `int func(int (*B)[10])` – newacct Dec 05 '12 at 21:04

9 Answers9

43

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

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
12

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;
}
Alberto Bonsanto
  • 17,556
  • 10
  • 64
  • 93
  • if i try to edit B[2] in func i get some errors... code: `*B[2] = 5;` – stergosz Dec 05 '12 at 19:27
  • @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;` – Ed S. Dec 05 '12 at 19:35
  • 2
    This is the answer I was looking for. Why would you do `array + 2` instead of `array[2]`? – Iulian Onofrei Jan 13 '17 at 09:32
8

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;
}
greggo
  • 3,009
  • 2
  • 23
  • 22
  • And I note that C++ is getting templateable typedefs, so it should be possible to write ptr_to_arr_of select_myarr_row ... . – greggo Dec 13 '13 at 15:37
2

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;
}
David Buck
  • 3,752
  • 35
  • 31
  • 35
1

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.

Sunil Bojanapally
  • 12,528
  • 4
  • 33
  • 46
0
main()
{
    int *arr[5];
    int i=31, j=5, k=19, l=71, m;

    arr[0]=&i;
    arr[1]=&j;
    arr[2]=&k;
    arr[3]=&l;
    arr[4]=&m;

    for(m=0; m<=4; m++)
    {
        printf("%d",*(arr[m]));
    }
    return 0;
}
t3dodson
  • 3,949
  • 2
  • 29
  • 40
0

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.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
G_L
  • 103
  • 5
0

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;
}
Watercayman
  • 7,970
  • 10
  • 31
  • 49
Jay Gupta
  • 1
  • 1
-2

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
CRABOLO
  • 8,605
  • 39
  • 41
  • 68