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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:22:28+00:00 2026-05-10T14:22:28+00:00

In C can I pass a multidimensional array to a function as a single

  • 0

In C can I pass a multidimensional array to a function as a single argument when I don’t know what the dimensions of the array are going to be?

Besides, my multidimensional array may contain types other than strings.

  • 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. 2026-05-10T14:22:28+00:00Added an answer on May 10, 2026 at 2:22 pm

    You can do this with any data type. Simply make it a pointer-to-pointer:

    typedef struct {   int myint;   char* mystring; } data;  data** array; 

    But don’t forget you still have to malloc the variable, and it does get a bit complex:

    //initialize int x,y,w,h; w = 10; //width of array h = 20; //height of array  //malloc the 'y' dimension array = malloc(sizeof(data*) * h);  //iterate over 'y' dimension for(y=0;y<h;y++){   //malloc the 'x' dimension   array[y] = malloc(sizeof(data) * w);    //iterate over the 'x' dimension   for(x=0;x<w;x++){     //malloc the string in the data structure     array[y][x].mystring = malloc(50); //50 chars      //initialize     array[y][x].myint = 6;     strcpy(array[y][x].mystring, 'w00t');   } } 

    The code to deallocate the structure looks similar – don’t forget to call free() on everything you malloced! (Also, in robust applications you should check the return of malloc().)

    Now let’s say you want to pass this to a function. You can still use the double pointer, because you probably want to do manipulations on the data structure, not the pointer to pointers of data structures:

    int whatsMyInt(data** arrayPtr, int x, int y){   return arrayPtr[y][x].myint; } 

    Call this function with:

    printf('My int is %d.\n', whatsMyInt(array, 2, 4)); 

    Output:

    My int is 6. 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working with EmguCV and I don't know how I can pass array
I know you can pass arguments through the RunWorkerAsync function call when you first
How can I pass a multdimensional array of bools to a function in F#?
We can pass a single property through java CLI using -D argument: java -Dkey=value
Possible Duplicate: How to pass a multidimensional array to a function in C and
I need to pass a js multidimensional array (the dimensions are not known at
I know that I can pass a string as the second parameter to the
I know from here that I can pass an ignore option in the jQuery.validate
name_iexact keyword is a parameter we can pass to filter function in django.model. Can
Currently, I'm trying to pass a multidimensional array in Phonegap between two pages with

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.