By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,501 Members | 2,871 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,501 IT Pros & Developers. It's quick & easy.

How to define a function that returns a pointer to an array?

itiger
P: 12
give a sample example of it.
Sep 3 '10 #1

✓ answered by Banfa


Share this Question
Share on Google+
5 Replies


P: 39
Something like this ?
Expand|Select|Wrap|Line Numbers
  1. int* getArray();
Sep 3 '10 #2

Banfa
Expert Mod 5K+
P: 8,916
Read this
Sep 3 '10 #3

itiger
P: 12
your function declaration returns pointer to an integer variable not a 'pointer to an int array'.
Sep 3 '10 #4

P: 39
your function declaration returns pointer to an integer variable not a 'pointer to an int array'.
They are in fact the same, please do click on the link Banfa gave you.
Sep 3 '10 #5

weaknessforcats
Expert Mod 5K+
P: 9,197
They are in fact the same, please do click on the link Banfa gave you.
Not true. An int pointer is not a pointer to an array. An int pointer is a pointer to a single int.

In both C and C++ when you refer to an array by using the address of element 0, you lose the number of elements and end up with only the address of element 0. This is called decay of array and because of this you also need the number of elements.

Also, in C and C++, functions have a return type which can be a) a type or b) a pointer to a type. The most you can return is the address of an element of the array.
Sep 4 '10 #6

Post your reply

Sign in to post your reply or Sign up for a free account.