Thread: Alias to return an array

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    178

    Alias to return an array

    Not making sense to me. Ok, I have defined an alias to in as:

    Code:
    typedef int arrT[10];
    I understand arrT to be an alias for an int[10]; Arrays convert to pointers to the first element. Thus, if I define a function as such:

    Code:
    arrT *func(int i);
    arrT *func(int i){
    
          static int arr[i];
          int *parr = arr;
          return  parr;
    }
    This is wrong as I can't return a pointer to int because it is expecting an int (*)[] to be returned. What am I not getting here?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, you are telling the compiler that you are returning a pointer to an array of 10 elements, so:
    Code:
    typedef int arrT[10];
    
    arrT* func(int i);
    arrT* func(int i)
    { 
    	static int arr[10];
    	return &arr;
    }
    Btw, you cannot initialize the array with a variable "i". This is not allowed in ISO C++.
    If you need to do so, you should use a vector:

    Code:
    typedef vector<int> arrT;
    
    arrT& func(int i);
    arrT& func(int i)
    { 
    	static std::vector<int> arr(i);
    	return arr;
    }
    But why are you trying to return an array in this manner?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    I do not see the point in making the internal vector static unless you want all the pointers returned to point to the same vector.

    In fact, why not just --
    Code:
    vector<int> func(int i) {
       vector<int> arr(i);
       return arr;
    }
    If you are going to completely change everything? Vectors are copyable after all.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    178
    Thank you for the quick responses. The intent is not to play with arrays in C++ instead of vectors but if I need to play with arrays in C++ and perhaps return one from a function, I know ways in which I can do that.

    My confusion was with int* and int (*)[]; the conversion being impossible and I just did not know how to resolve this.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    178
    Quote Originally Posted by whiteflags View Post

    In fact, why not just --
    Code:
    vector<int> func(int i) {
       vector<int> arr(i);
       return arr;
    }
    Should the vector not be static since you are returning a vector that is local to the block and when func returns, the memory is reused?

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    178
    Quote Originally Posted by Elysia View Post
    Well, you are telling the compiler that you are returning a pointer to an array of 10 elements, so:
    Code:
    typedef int arrT[10];
    
    arrT* func(int i);
    arrT* func(int i)
    { 
    	static int arr[10];
    	return &arr;
    }
    Btw, you cannot initialize the array with a variable "i". This is not allowed in ISO C++.
    If you need to do so, you should use a vector:

    Code:
    typedef vector<int> arrT;
    
    arrT& func(int i);
    arrT& func(int i)
    { 
    	static std::vector<int> arr(i);
    	return arr;
    }
    But why are you trying to return an array in this manner?
    Thank you, I agree a vector is a better solution and I understand that vector should be used over array but for my own knowledge, I am choose to return a pointer to an array.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Imanuel View Post
    Thank you for the quick responses. The intent is not to play with arrays in C++ instead of vectors but if I need to play with arrays in C++ and perhaps return one from a function, I know ways in which I can do that.

    My confusion was with int* and int (*)[]; the conversion being impossible and I just did not know how to resolve this.
    You can also use a C++ array to do it:
    Code:
    std::array<int, 10> func(int i)
    {
    	std::array<int, 10> arr({ /* initialize it here */});
    	return arr;
    }
    This sample requires C++11 for initializer lists and TR1 or C++11 for std::array. See my sig for information on how to enable C++11 for your compiler.

    Quote Originally Posted by Imanuel View Post
    Should the vector not be static since you are returning a vector that is local to the block and when func returns, the memory is reused?
    No. Since a vector is an object, a copy of the object is made upon returning, so it is fine.
    (We don't need to worry about how a vector works internally.)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    178
    yes, saw std::array<int, 10> in another post. Ok you guys rock and thank you so much for clearing this up.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alias for class template
    By rudyman in forum C++ Programming
    Replies: 2
    Last Post: 07-26-2008, 04:24 PM
  2. I don't understand this alias
    By cdalten in forum C Programming
    Replies: 4
    Last Post: 01-26-2006, 05:43 PM
  3. question in alias parameter`
    By ssharish in forum C++ Programming
    Replies: 1
    Last Post: 02-13-2005, 07:16 AM
  4. signiture and alias pic
    By Klinerr1 in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-05-2002, 01:52 PM
  5. Alias...
    By Cheeze-It in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-01-2001, 04:25 PM