4

I am learning C++ from Primer 5th edition and I am at Returning a Pointer to an Array. The declaration of this function is:

 int (*func(int i))[10]; 

and it's expected to return a pointer to an array.

I wrote code that does this:

#include <iostream>
#include <string>
using namespace std;

int *func(){
  static int a[]={1,2,3};
  return a;
}
int main (){
  int *p=func();
  for(int i=0;i!=3;++i){
    cout<<*(p+i);
  }
}

And it is working. But I want to know the difference between what I made here and

  int (*func(int i))[10]; 

How I can make this function call work, because in the book, there isn't any concrete example.

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
alin
  • 65
  • 1
  • 6
  • Yes because i don t understand this type of function and i made this code, and working but i want to work int (*func(int i))[10]; – alin Jul 26 '13 at 13:10
  • 1
    I think you are confuse between [array name = `a` , `int*`and address of array `&a`, `int(*)[5]`](http://stackoverflow.com/questions/15177420/what-does-sizeofarr-return/15177499#15177499) read lined answer may help you. – Grijesh Chauhan Jul 26 '13 at 13:11
  • 4
    @jrok: This question seems better than the other, I don't know why the votes to close, as it is well formed (well, on the average of the quality of the site I'd say) – David Rodríguez - dribeas Jul 26 '13 at 13:11
  • 1
    you are returning address of first element. Actually type of `a` is `int[3]` that decays into `int*`. Important is you stores address into `int* p` and can assess elements of array as `p[i]`. Whereas if your function would be `int (*func(int i))[3]` then you return `&a` and assign to `int(*p)[3]` and can access `(*p)[i]` – Grijesh Chauhan Jul 26 '13 at 13:14
  • @DavidRodríguez-dribeas Yeah, I feel a bit bad now :) voting to reopen. – jrok Jul 26 '13 at 13:16
  • Can you explicitly state the difference? I don't see it. – Jacob Jul 26 '13 at 13:35
  • @GrijeshChauhan thank you very much, but for 20 minutes i reading and rereading your comment and i can t understand. Can you make like a code who working, because I can t understand where comes every declaration . – alin Jul 26 '13 at 13:41
  • 1
    @alin let me know does my answer helps, ask your doubt to my answer.. feel free to ask .. – Grijesh Chauhan Jul 26 '13 at 14:02
  • @alin In you previous question [you got the answer](http://stackoverflow.com/a/17871039/1673391) that no buddy notice, and voted. – Grijesh Chauhan Jul 26 '13 at 14:39
  • @GrijeshChauhan Thank you very much for your answer, it helped me and i understan in finally, but i have one more question, how i escape by static int a[]{}, so, I want to return a array not from fuction, by exemple from int main(), or anywere – alin Jul 26 '13 at 14:47
  • 1
    @alin (1) comment to answer instead to your question (else it will confuse other). (2) I couldn't understand your question --but Understand if you declares your array simply `int a[] = {1, 3, 3}` instead of using `static` keyword then is becomes buggy code. because life & scope of local array is till function not returns. – Grijesh Chauhan Jul 26 '13 at 14:51
  • yes i know that if i don t use static it is wrong, it says this in book and i understand this, but i want to know if is any possibilities to return other array. – alin Jul 26 '13 at 14:55
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/34242/discussion-between-alin-and-grijesh-chauhan) – alin Jul 26 '13 at 14:57

2 Answers2

3

Read: What does sizeof(&array) return? to understand diffrence between array name and address of array.

Q1 I want to know the difference between:  

In your code:

  int *func(){
    static int a[]={1,2,3};
    return a;
  }

you are returning address of first element. Actually type of a is int[3] that decays into int*. Important is
You stores address into int* p and can assess elements of array as p[i].

Whereas if your function would be int int (*func())[3] then you return &a, and assign to int(*p)[3] and can access (*p)[i].
Note: type of &a is int(*)[3].

Q2 How i can make this function call work, because in the book, there isn't any concrete example.

like:

int (*func())[3]{
    static int a[]={1,2,3};
    return &a;
}

And main():

int main(){ 
 int i=0;    
 int(*p)[3] = func();
 for(i=0; i<3; i++)
   printf(" %d\n", (*p)[i]);
 return 0;
}

You can check second version of code working id Ideone

Q1 I want to know the difference between:  

As you are interested to know diffrence between two so now compare two different declarations of p in two versions of code:

1) : int* p; and we access array elements as p[i] that is equals to *(p + i).

2) : int (*p)[i] and we access array elements as (*p)[i] that is equals to *((*p) + i) or just = *(*p + i). ( I added () around *p to access array element because precedence of [] operator is higher then * So simple *p[i] means defense to the array elements).

Edit:

An addition information other then return type:

In both kind of functions we returns address that is of a static variable (array), and a static object life is till program not terminates. So access the array outsize func() is not a problem.

Consider if you returns address of simple array (or variable) that is not static (and dynamically allocated) then it introduce as Undefined behavior in your code that can crash.

Community
  • 1
  • 1
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
  • Thank you very much for your answer, it helped me and i understan in finally, but i have one more question, how i escape by static int a[]{}, so, I want to return a array not from fuction, by exemple from int main(), or anywere – alin Jul 26 '13 at 14:48
  • 1
    @alin see `main()` is quit different function then other its declaration is predefined you can't make a `main()` that returns pointer to array. Its return type has to be int only. – Grijesh Chauhan Jul 26 '13 at 14:53
  • Sorry @ Grijesh chauhan, but when I "discover" this function (accept an answer) I give accept at all, and first was your answer, and when i accept any answer your answer was unaccepted. Sorry , i accept your answer. – alin Jul 26 '13 at 20:25
  • @alin Ok no problem, You can vote all, but can accept only one that the reason ?I give a like to you. – Grijesh Chauhan Jul 26 '13 at 20:30
  • @GrijeshChauhan i was reading your answer then i wanted to try your second and third code but i could not compile it (it tells me that Returning 'int[3] *' from a function/method/block returning 'int[3]': Array is not assignabel ) – Mohammad Abdollahzadeh Jul 17 '17 at 10:30
2

int(*)[10] is a pointer to an array of 10 ints. int* is a pointer to int. These are different types.

However, an array decays to a pointer to its first element, so you can do:

int a[10];
int(*p)[10] = &a;
int* q = a; // decay of int[10] to int*

But not:

q = p;
p = q;
Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271