0

I have in my main-Function an double Array:

double ary1D[4] = {1.1, 2.2, 3.3, 4.4};

and have a function call:

print1D_A(&ary1D[0],Num);

my function:

void print1D_A(double *ary1D, int Num);

so my question is, Whats is the difference between &ary1D[0] and &ary1D. My compiler give me an error, if I call the function with &ary1D. But the argument &ary1D is the first Adress of my Array, just like &ary1D[0].

Can I change the argumentlist of my function, that I can call it with

print1D_A(&ary1D,Num);
Lundin
  • 195,001
  • 40
  • 254
  • 396
Fkcm95
  • 29
  • 2
  • 1
    Care to share that error? As a new user, please also take the [tour] and read [ask]. Concerning your code, it's also customary to extract a [mcve]. BTW: C++ is more strict than C, which often lets you off with a warning. Make sure you read and understand said warning! – Ulrich Eckhardt Mar 13 '19 at 06:51
  • All of these are the same address, `ary1D`, `&ary1D[0]` and `&ary1D`. The difference is the **type** of the pointer. The first two are a pointer to the first element (i.e. `double*`), the last is a pointer to the array itself, i.e. `double (*)[4]` – john Mar 13 '19 at 06:52
  • 6
    Note that many of us "oldies" dislike the term "C/C++". There is no language "C/C++", only the two (very) different languages C and C++. And while some things might seem similar in both languages, there might be minute differences in the specifications that means the behavior could be vastly different. – Some programmer dude Mar 13 '19 at 06:55
  • Answer to your second question is yes, change your function signature to `void print1D_A(double (*ary1D)[4], int Num);` but really it's not a good idea. – john Mar 13 '19 at 06:57
  • *Why* do you want to change the signature of the function? *Why* do you want to pass a pointer to the array? What problem is that supposed to solve? What is you *real* and *actual* problem that lead you to write this question? – Some programmer dude Mar 13 '19 at 07:01
  • 1
    Wow, thanks! I have no problem. I want to understand, how arrays/pointers are work.. Thank you all.. – Fkcm95 Mar 13 '19 at 07:03
  • 1
    @Fkcm95 It's good to want to learn these things but practically pointers to arrays are very obscure, and almost never used. – john Mar 13 '19 at 07:05

1 Answers1

4

The expression &ary1D[0] is a pointer to the first element of ary1D. Its type is double*. This is also what plain ary1D decays to, so you could just do

print1D_A(ary1D,Num);

The expression &ary1D is a pointer to the array. Its type is double (*)[4].

Now, both pointers are pointing to the same location, but as you can see their types are very different. That could lead to errors if a function is expecting one type and you give the other.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • @Fkcm95 You need to make it accept the type `double(*)[4]`. But then you can't pass `&ary1D[0]` (or `ary1D`) as the type from that expression will no longer match. – Some programmer dude Mar 13 '19 at 07:01