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

  • Home
  • SEARCH
  • 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 6387955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:13:08+00:00 2026-05-25T03:13:08+00:00

I am trying to write a function that takes an array of an variable

  • 0

I am trying to write a function that takes an array of an variable size in c.

void sort(int s, int e, int arr[*]){
    ...
}

It says that for variable length arrays, it needs to be bounded in the function declaration. What does that mean? I am using xcode 4.0, with the LLVM compiler 2.0.

Thanks for the help.

  • 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-25T03:13:08+00:00Added an answer on May 25, 2026 at 3:13 am

    If you’re not using the C99 variable length arrays (it appears you are, so see below), the usual solution is to pass in a pointer to the first element, along with any indexes you want to use for accessing the elements.

    Here’s a piece of code that prints out a range of an array, similar to what you’re trying to do with your sort.

    #include <stdio.h>
    
    static void fn (int *arr, size_t start, size_t end) {
        size_t idx;
        for (idx = start; idx <= end; idx++) {
            printf ("%d ", arr[idx]);
        }
        putchar ('\n');
    }
    
    int main (void) {
        int my_array[] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
        fn (my_array, 4, 6);
        return 0;
    }
    

    This outputs elements four through six inclusive (zero-based), giving:

    5 4 3
    

    A couple of points to note.

    • Using my_array in that function call to fn automatically "decays" the array into a pointer to its first element. This actually happens under most (not all) circumstances when you use arrays, so you don’t have to explicitly state &(my_array[0]).

    • C already has a very good sort function built in to the standard library, called qsort. In many cases, that’s what you should be using (unless either you have a specific algorithm you want to use for sorting, or you’re doing a homework/self-education exercise).


    If you are using real VLAs, you should be aware that the [*] construct is only valid in the function prototype, not in an actual definition of the function.

    So, while:

    void xyzzy(int, int[*]);
    

    is valid, the following is not:

    void xyzzy(int sz, int plugh[*]) { doSomething(); }
    

    That’s because, while you don’t need the size parameter in the prototype, you do very much need it in the definition. And, since you have it, you should just use it:

    void xyzzy(int sz, int plugh[sz]) { doSomething(); }
    

    The gcc compiler actually has a reasonably clear error message for this, far better than the "needs to be bounded in the function declaration" one you saw:

    error: ‘[*]’ not allowed in other than function prototype scope

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a Powershell function that takes an array argument. I want
I'm trying to write a simple Vim function that takes the name of a
I'm trying to write a function that formats every (string) member/variable in an object,
I am trying to write a function that takes a pointer argument, modifies what
I'm trying to write a function that takes a line as a string and
I am trying to write a function in Matlab that takes an RGB image
I've been trying to write a Table-Valued function that takes value pairs as a
I'm trying to write a Java function that takes a Class<?> and returns a
I am trying to write a translation function that takes the value and the
I'm trying to write a php function that takes the $name and $time and

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.