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 6901987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:44:32+00:00 2026-05-27T07:44:32+00:00

What would the prototype declaration of a C function look like that returns an

  • 0

What would the prototype declaration of a C function look like that returns an n-dimensional array?

Here, n>=2.

Please explain it with dynamic-array/pointers.

My idea is not to pass any-dimensional array.

Please give me 3 examples with 2, 3 and 4 dimensions.

That will be enough for me to grab the idea.

  • 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-27T07:44:32+00:00Added an answer on May 27, 2026 at 7:44 am

    I would suggest this:

    array foo();
    

    where array is an struct defined as:

    struct array
    {
        int *data;
        unsigned int ndim;  //number of dimensions
        unsigned int *size; //size of each dimension is size[i].
    };
    

    Of course, data is just a pointer to int, but the other two fields in the struct can be used in such a way that the data can be interpreted like an n dimensional array, and size of each dimension can be stored in size which is yet another pointer.

    For example, take 4-dimensional array (dimension size are 10, 20, 30 and 40), then you can create and initialize 4D array as:

    unsigned int size[] = {10,20,30,40};
    array arr = create(4, size);
    

    where create function is defined as:

    array create(unsigned int n, unsigned int *size)
    {
        array arr;
        arr.ndim = n;
        arr.size = (unsigned int*) malloc(n * sizeof(unsigned int));
        int i;
        unsigned int totalElements =  1;
        for( i = 0 ; i < n ; ++i) 
        {
              arr.size[i] = size[i]; 
              totalElements *= size[i]; 
        }
        arr.data = (unsigned int*) malloc(totalElements * sizeof(int));
        return arr;
    }
    

    Of course, you’ve to work a lot with the fields of the struct, to make it look like n-dimensional array. You don’t need to follow me exactly the way I explained, but this is just a basic idea. You can modify it, to suit your specific need.

    I would suggest you to write few functions, to manipulate the array, and to access the array elements. I’ve written create function, likewise here is destroy function:

    void destroy(array arr)
    {
        if (arr.size != NULL && arr.data != NULL) 
        {
           free(arr.size);
           free(arr.data);
           arr.size = arr.data = NULL;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Prototype here and would like to build a simple toggler that toggles
I would like to make this form dynamic with prototype. I mean, the radio
Would having a nice little feature that makes it quicker to write code like
I am looking for some tool that would help me prototype basic website structure
I would like to know if there is any special way that one would
I would like to read the values of HTML td using prototype. For example,
I would like to know how I could use jQuery inside a native prototype
I have an array of structs. The struct has two function pointers. Each element
I would like to prototype an object but the problem is, I don't know
Hello all! I'm working on a prototype that would require me being able to

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.