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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:14:36+00:00 2026-06-16T15:14:36+00:00

Possible Duplicate: How do I use arrays in C++? One dimensional array as a

  • 0

Possible Duplicate:
How do I use arrays in C++?

One dimensional array as a function parameter:

#include <stdio.h>
#include <string.h>

int func(int a[], int n)
{
    int i;
    for(i = 0; i < n; i++)
        printf("%d ", a[i][j]);
}

int main(void)
{   
    int a[2] = {1,2};
    func(a, 2);
}

it compiles and runs correctly.

But when a two-dimensional array as a function parameter:

#include <stdio.h>
#include <string.h>

int func(int a[][], int n)
{
    int i, j;
    for(i = 0; i < n; i++)
        for(j = 0 ; j < n; j++)
            printf("%d ", a[i][j]);
        printf("\n");
}

int main(void)
{   
    int a[2][2] = {{1,2}, {3,4}};
    func(a, 2);
}

it can not compile correctly. I must modify the code like this:

#include <stdio.h>
#include <string.h>

int func(int a[][2], int n)
{
    int i, j;
    for(i = 0; i < n; i++)
        for(j = 0 ; j < n; j++)
            printf("%d ", a[i][j]);
        printf("\n");
}

int main(void)
{   
    int a[2][2] = {{1,2}, {3,4}};
    func(a, 2);
}

I do not know why? Anybody can explain how it works? Many thanks.

  • 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-06-16T15:14:37+00:00Added an answer on June 16, 2026 at 3:14 pm

    Arrays (both one and multidimensional) in c reside in continuous memory blocks. This means that when you define char a[3], the array is laid out in memory like this (forgive my terrible ascii art skills):

    | a[0] | a[1] | a[2] |
    

    For a two-dimensional array char a[2][3], the layout is like this:

    | a[0][0] | a[0][1] | a[0][2] | a[1][0] | a[1][1] | a[1][2] |  
                                  ^
                                  +--- first row ends here
    

    Therefore, when you index into a two-dimensional array a[i][j], the compiler generates code equivalent to this:

    *(a + i*3 + j)
    

    Which can be read as “skip i rows and take cell j in that row”. To accomplish this, the compiler must know the length of the row (which is the second dimension). This means that the second dimension is a part of the type definition!

    As such when you want pass a 2d array into a function, you must specify the needed dimension for the type definition.

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

Sidebar

Related Questions

Possible Duplicate: how to use array of function pointers? I am wondering how one
Possible Duplicate: I have an array of integers, how do I use each one
Possible Duplicate: How to use include within a function? i have two file inc.php
Possible Duplicate: How do I use arrays in C++? Is 2d array a double
Possible Duplicate: Negative array indexes in C? Can I use negative indices in arrays?
Possible Duplicate: convert string to 2D array using php I have the string like
Possible Duplicate: How do I use arrays in C++? I am not sure if
Possible Duplicate: How do I use arrays in C++? In the following code snippet:
Possible Duplicate: Comparing Arrays in C# I have a two string arrays: string[] a;
I'm C++ begginer. I did this excercise from Deitel's book: Use a one-dimensional array

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.