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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • itiger
    New Member
    • Sep 2010
    • 12

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

    give a sample example of it.
  • MartijnHoekstra
    New Member
    • Sep 2010
    • 39

    #2
    Something like this ?
    Code:
    int* getArray();

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Read this

      Comment

      • itiger
        New Member
        • Sep 2010
        • 12

        #4
        your function declaration returns pointer to an integer variable not a 'pointer to an int array'.

        Comment

        • MartijnHoekstra
          New Member
          • Sep 2010
          • 39

          #5
          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.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            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.

            Comment

            Working...