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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:01:19+00:00 2026-05-24T07:01:19+00:00

I’m trying to write a function that will print out the contents of a

  • 0

I’m trying to write a function that will print out the contents of a multidimensional array. I know the size of the columns, but not the size of the rows.

EDIT: Since I didn’t make this clear, the arrays passed to this function are NOT dynamically allocated. The sizes are known at compile time.

I am testing it using a 3×2 array. Here is the function as it stands:

void printArrays(int array1[][2], int array2[][2]) {
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 2; j++) {
            cout << "\narray1[" << i << "][" << j << "] = "
                 << setfill('0') << setw(2) << array1[i][j]
                 << "\tarray2[" << i << "][" << j << "] = "
                 << setfill('0') << setw(2) << array2[i][j];
        }
    }
}

Obviously, this only works if I know the size of “i” is 3 (it is in this case). Ideally, however, I would like the function to work no matter what the size of the first dimension.

I thought I would be able to do this using the sizeof() function, e.g.

int size = sizeof(array1);

… and do some math from there.

Here’s the odd part. If I use the sizeof() function inside the array, it returns a value of 4. I can use pointer notation to dereference the array:

int size = sizeof(*array1);

… but this actually returns a value of 8. This is odd, because the total size should be rows(which = 3) * columns(= 2) * sizeof(int)(= 4), or 24. And, indeed, this is the result, when I use sizeof(*array1) outside of the function.

Does anyone know what is going on here? More importantly, does anyone have a solution?

  • 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-24T07:01:19+00:00Added an answer on May 24, 2026 at 7:01 am

    The answer is that you can not do this. You must pass the number of rows as an argument to the function, or use an STL container such as std::vector or std::array.

    sizeof is computed compile time; sizeof is never useful in determining dynamic size of objects in C/C++. You (yourself, the programmer) can always calculate sizeof(x) just from looking at code and header files since sizeof counts the number of bytes used to represent the object. sizeof(*array1) will always be 8 since array1[i] is an array of two ints and 4==sizeof(int). When you declare int array1[][2] this is equivalent to int *array1[2]. That is, array1 is a pointer to arrays of two integers. sizeof(array1) is therefore 4 bytes, since it takes 4 bytes on your machine to represent a pointer.

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

Sidebar

Related Questions

I need a function that will clean a strings' special characters. I do NOT
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want to construct a data frame in an Rcpp function, but when I
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.