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

  • Home
  • SEARCH
  • 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 1115817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T03:12:42+00:00 2026-05-17T03:12:42+00:00

Executive summary: How can I define an arbitrarily-sized 2D array in C? How can

  • 0

Executive summary:

  1. How can I define an arbitrarily-sized 2D array in C?
  2. How can I determine the dimensions of that array at compile-time?

Full disclosure:

I’m writing code for an embedded controller. My application requires several lookup tables with different sizes which will all be used by one lookup function (a binary search). Here is what I have so far:

typedef struct
{
    unsigned char count;        // number of rows in the table
    unsigned char width;        // number of bytes in each row
    const unsigned char * data; // pointer to table data[count][width]
}
LookupTable;

// returns the index of a value from within a table
unsigned char Lookup(unsigned long value, const LookupTable * table);

This part is working. What I would like to do now is define these tables in my source without having to manually enter the count and width constants. Here is what I am doing now:

#define T1_count 100
#define T1_width 3
const unsigned char table1_data[T1_count][T1_width] = 
{
    { 0x12, 0x34, 0x56 },
    { 0x12, 0x38, 0x12 },
    ...
};

const LookupTable table1 = { T1_count, T1_width, table1_data };

Here is what I would like to be able to do (pseudo-code, since this array definition will not actually compile):

const unsigned char table1_data[] = 
{
    { 0x12, 0x34, 0x56 },
    { 0x12, 0x38, 0x12 },
    ...
};

const LookupTable table1 =
{
    get_count_expr(table1_data),
    get_width_expr(table1_data),
    table1_data
};

Obviously, get_count_expr and get_width_expr would have to be constant expressions of some sort, based on the size of the table, and not actual function calls.

To be clear, no part of this design is cast in stone. I’m just posting what I have so far, in the hopes that my intent is clear. Any ideas for improvement would be appreciated.

The “why”:

These tables will be changed often, and it would make maintenance easier if entries could be added and removed, or the width of a table changed without having to manually adjust the constants each time. Having to keep track of the sizes manually can be error-prone and violates DRY. I’m looking for a better way.

  • 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-17T03:12:43+00:00Added an answer on May 17, 2026 at 3:12 am

    Hmmm … you can leave the leftmost size to the compiler:

    #define T1_WIDTH 3
    const unsigned char table1_data[][T1_WIDTH] = 
    {
        { 0x12, 0x34, 0x56 },
        { 0x12, 0x38, 0x12 },
        /* ... */
    };
    T1_count = sizeof table1_data / sizeof *table1_data;
    T1_width = sizeof *table1_data;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Executive Summary: When assertion errors are thrown in the threads, the unit test doesn't
During execution, how can a java program tell how much memory it is using?
Executing this code: mainLyr = [[CALayer layer] retain]; [mainLyr setFrame:CGRectMake(0.0,0.0,23.0,23.0)]; in debugger, I found
When executing SubmitChanges to the DataContext after updating a couple properties with a LINQ
When executing the following (complete) SQL query on Microsoft SQL Server 2000: SELECT B.ARTIFACTTNS,
When you execute a SQL query, you have to clean your strings or users
How do I execute the Copy Web Site command for an ASP.NET project in
I need to execute a large set of SQL statements (creating a bunch of
I need to execute a directory copy upon a user action, but the directories
I'd like to execute JavaScript code from within a C# assembly and have the

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.