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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:52:47+00:00 2026-05-14T14:52:47+00:00

I have some problems using two dimensional array. static const int PATTERNS[20][4]; static void

  • 0

I have some problems using two dimensional array.

static const int PATTERNS[20][4];

static void init_PATTERN()
{
   // problem #1
   int (&patterns)[20][4] = const_cast<int[20][4]>(PATTERNS);  
   ...
}

extern void UsePattern(int a, const int** patterns, int patterns_size);

// problem #2
UsePattern(10, PATTERNS, sizeof(PATTERNS)/sizeof(PATTERNS[0]));

In the first statement, I need to cast the const off the two dimensional array PATTERNS. The reason for this is that the init function is called only once, and in the remaining code, PATTERNS is strictly read-only.

In the second statement, I need to pass PATTERNS array to the int** argument. Direct passing resulted a compile error.


I have solved the problem, just about the same time that @Andrey posted the answer. Yes int[][] can’t be casted to int**.

It can be casted to int* through &(PATTERNS[0][0]), and the function prototype must be modified with row size (the number of elements in a row). The array can be const_cast away with reference syntax.

  • 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-14T14:52:48+00:00Added an answer on May 14, 2026 at 2:52 pm

    Firstly, there’s no such thing as cast to array type (or to function type) in C++. Yet this is what you are trying to do. If you want to cast away constness from something, you have to cast to either pointer or reference type. In your case you have a reference on the receiving end of the cast, so the cast itself has to be to reference type as well

    int (&patterns)[20][4] = const_cast<int (&)[20][4]>(PATTERNS); 
    

    Of course, as Bill already noted, casting away constness from a constant object (and then attempting to modify the object) leads to undefined behavior.

    Secondly, a two-dimensional array cannot be passed anywhere as an int ** pointer. If you want to pass your PATTERNS somewhere, you can pass it as const int (&)[20][4], const int (*)[20][4], const int [][4], const int (*)[4] or something similar to that, but not as int **. Do a search on SO and/or read some FAQ on arrays to understand why. This has been explained too many times to repeat it again.

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

Sidebar

Related Questions

I am learning Java with Swing and I have some problems with using JTextField
I have some problems to start programming using cocos2D on linux + android. I
I have some problems sending mails through SMTP using Spring's MailSender interface and the
I have some problems with a java app i'm developing, i'm using HtmlCleaner 2.2
I'm having some problems using cascade operations on hibernate. I have one entity (TripleDBmodel),
I am using the new ASP control Chart, but I have some problems with
We have some performance problems with one of our applications. I thought about using
I have some strange scrolling problems with my PhoneGap (using jQuery Mobile) app: I'm
I have some problem with the cursor when using JOprionPane. I set a cursor
I have some problem to get the utf-8 string from PHP using jQuery $.load()

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.