Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6657641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:47:38+00:00 2026-05-26T01:47:38+00:00

Why is this valid in C int * foo(int a,int b){ … } but

  • 0

Why is this valid in C

int * foo(int a,int b){
...
}

but this is invalid

int [] foo(int a,int b){
...
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:47:38+00:00Added an answer on May 26, 2026 at 1:47 am

    The syntax is somewhat funny.

    int foo(int a, int b) []
    {
        ...
    }
    

    But it’s not allowed anyway. See n1256 6.7.5.3 paragraph 1, “Function declarators”.

    A function declarator shall not specify a return type that is a function type or an array type.

    You can return pointers to arrays:

    int (*foo(int a, int b)) []; // Weird syntax, n'est-ce pas?
    

    But you might as well just return a pointer instead, since the following are equivalent:

    int array[] = { ... };
    int *x = array, (*y)[] = &array;
    x[0];
    (*y)[0]; // same as above, just with more cumbersome syntax
    

    Typically, if a function needs to return an array of int, you either return the pointer or pass the pointer in. One of the following:

    int *func(int a, int b); // Allocated by func
    void func(int a, int b, int *array); // Allocated by caller
    void func(int a, int b, int **array); // Allocated by func
    

    The “struct-hack” also works for arrays that have a fixed size:

    struct { int arr[50]; } array_struct;
    struct array_struct func(int a, int b);
    

    But this is not recommended unless the array is small.

    Rationale:

    Arrays are often large, and often have a size not known until runtime. Since parameters and return values are passed using the stack and registers (on all ABIs I know of), and the stack has a fixed size, it is somewhat dangerous to pass such a large object around on the stack. Some ABIs also don’t gracefully handle large return values, potentially causing extra copies of return values to be generated.

    The following code can also be dangerous:

    void func(...)
    {
        int arr[BIG_NUMBER]; // potential for stack overflow
        int *ptr = alloca(sizeof(int) * BIG_NUMBER); // potential for stack overflow
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is this valid? public struct MyStruct { public int Foo { get; set; }
Is this list-initialization of an array of unknown size valid in C++0x? int main()
Is the following code valid? class Foo() { int* Bar; public: Foo() { *Bar
Is this example code valid? #include<vector> using namespace std; int main() { vector<int> vec(10);
Let us say I have: // This is all valid in C++11. struct Foo
Is this valid C (C99) code? int f(); int g(int x) { if (x<0)
Is this valid and correct? RewriteRule ^myOldPage.html$ /index.php#info [R] I'm specifically interested about the
Is this valid: $_SESSION['pictures']['rateAlbum']['_POST'] = $_POST; I want to save all of the POST
Is this valid C++ (e.g. not invoking UB) and does it achieve what I
As you know the dash introduces a comment how can I make this valid?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.