-2

I want to get the value from a Reference in function. My Function is:

void print2D_A(double (*ary2D)[3][4]){ ... }

Main:

double ary2D[3][4] = {
                        {10.1, 11.2, 12.3, 13.4},
                        {20.1, 21.2, 22.3, 23.4},
                        {30.1, 31.2, 32.3, 33.4}
                     };
print2D_A(&ary2D);

Get First Value in Function:

*(ary2D)+1
YosiFZ
  • 7,792
  • 21
  • 114
  • 221
Fkcm95
  • 29
  • 2
  • Don't use pointer arithmetic directly, use array-indexing properly. That will make your code much cleaner and easier to read and understand. And as mentioned in your [previous question](https://stackoverflow.com/questions/55135942/difference-ary1d0-and-ary1d) don't pass pointers to arrays (it's *very* seldom needed). – Some programmer dude Mar 13 '19 at 10:06
  • And what is the problem? – klutt Mar 13 '19 at 10:08
  • Ok, if I write in the function : cout << ary2D[1][1] << endl; I get the address. – Fkcm95 Mar 13 '19 at 10:08
  • As for how to solve what I think your problem is, remember that C doesn't have "2d" arrays. What you have here is an array *of arrays*. To help you understand how they work I suggest you draw it out on a piece of paper, and then use arrows to draw out pointers to different places. – Some programmer dude Mar 13 '19 at 10:08
  • Problem is, I get the address and not the value.. – Fkcm95 Mar 13 '19 at 10:08
  • The function should usually be `void print2D_A(double ary2D[][4]){ ... }` called with `print2D_A(ary2D);` and the first element is accessed with `ary2D[0][0]` – Weather Vane Mar 13 '19 at 10:09
  • @Fkcm95 the function thinks it is getting a 2D array of pointers. – Weather Vane Mar 13 '19 at 10:10
  • Or perhaps the problem is that you forget that `ary2D` is a *pointer*, and that it needs to be dereferenced to get what it points to. So you need to use e.g. `(*ary2D)[0][0]` to get the first element of the first array (the parentheses are needed because of [*operator precedence*](https://en.cppreference.com/w/c/language/operator_precedence)). – Some programmer dude Mar 13 '19 at 10:11

1 Answers1

0

Do not use the address of a 2D array, it really not clearly to understand.

void print2D_A(double ary2D[3][4])    // `ary2D` is already a pointer of array, seems like `douible* ary2D`
{
    printf("%f\n", ary2D[0][0]);
}

EDIT:

void print2D_B(double ary2D[3][4])    // `ary2D` is already a pointer of array, seems like `douible* ary2D`
{
    for(int i=0; i<3; i++)
    {
        for(int j=0; j<4; j++)
        {
            printf("%f, ", ary2D[i][j]);
        }
        printf("\n");
    }
}
int main(int argc, char *argv[])
{
    double ary2D[3][4] = {
        { 10.1, 11.2, 12.3, 13.4 },
        { 20.1, 21.2, 22.3, 23.4 },
        { 30.1, 31.2, 32.3, 33.4 }
    };
    print2D_A(ary2D);    // In fact, this is a pointer of double array
    return 0;
}
Yuanhui
  • 459
  • 5
  • 15
  • The idiomatic declaration is more `void print2D_A(double ary2D[][4])`, because the 2D array will *decay* to a pointer to 1D arrays. – Serge Ballesta Mar 13 '19 at 10:21
  • First of all, Thanks for the answer. And how can I called the function in this way? print2D_A(&ary2D); How I must change the printf in the function?? – Fkcm95 Mar 13 '19 at 10:22
  • @Fkcm95 Again I wonder *why?* Is it still curiosity? Even after the comments and answers you got from your previous question? An array is an array is an array, it doesn't really matter if it's an array of `double`, or an array of *arrays* of `double`. The semantics and behavior is just the same. – Some programmer dude Mar 13 '19 at 11:05