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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T10:04:51+00:00 2026-05-19T10:04:51+00:00

I’m having problems passing a multidimensional array to a function in C. Both in

  • 0

I’m having problems passing a multidimensional array to a function in C. Both in the typedef and what the argument should be.

Currently, my function def looks like this:

int foo(char *a, char b[][7], int first_dimension_of_array);

I declare an array like this:

char multi_D_array[20][7];

When I try to call the function like this:

foo(d, multi_D_array, 20);

I get the warning that the second argument to the function is from incompatible pointer type. I’ve tried many of the variations that I’ve seen online but still can’t get it correct. Also, when using GDB, I find that only the first array of the 2D array is passed in. Feedback on what I’m doing wrong would be greatly appreciated.

  • 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-19T10:04:52+00:00Added an answer on May 19, 2026 at 10:04 am

    Big warning: I wouldn’t normally handle multi dimensional arrays this way.

    I tried this just to check, but this:

    #include <stdio.h>
    
    int foo(char b[][7])
    {
        printf("%d\n", b[3][4]);
        return 0;
    }
    
    int main(int argc, char** argv)
    {
        char multi_d_arr[20][7];
        multi_d_arr[3][4] = 42;
        foo(multi_d_arr);
        return 0;
    }
    

    Compiles and runs without any issue whatsoever, using gcc -Wall. I’m not sure, honestly, how the second argument can be considered an incompatible pointer type. Works fine.

    However, since you ask, how would I handle 2-D arrays? A better way is to use pointers like so:

    char** array2d = malloc((MAX_i+1)*sizeof(char*));
    for ( i = 0; i < (MAX_i+1); i++ )
    {
        array2d[i] = malloc((MAX_j+1)*sizeof(char));
    }
    

    Now, just to be clear, the greatest element you can access is array2d[MAX_i][MAX_j].

    Don’t forget to invert the process when done:

    for ( i = 0; i < (MAX_i+1); i++ )
    {
        free(array2d[i]);
    }
    free(array2d);
    

    Now, using this method, subscript notation remains valid, so array2d[x][y] still accesses the right value. Literally speaking, array2d is an array of pointers, and so is each array2d[i].

    Why is this a better method? Well, you can have variable size sub-arrays if you so like. All you have to do is change the for-loop. This techique is often used for arrays of strings, particularly in the int main(int argc, char** argv) signature of an app. argv is an array of an array of variable-length strings. Use strlen() to determine their length.

    Can you pass this around? Of course. You just received it in main, for starters, but you could also write your function signature like so:

    int foo(char** ar2d, const unsigned int size);
    

    and call it:

    char** somearray;
    /* initialisation of the above */
    
    foo(ar2d, thesizeofar2d);
    
    /* when done, free somearray appropriately */
    

    Just to be clear, your method of including size parameters (like const unsigned int size) is very good practise and you should stick to it.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have some data like this: 1 2 3 4 5 9 2 6
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites 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.