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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:13:53+00:00 2026-06-17T03:13:53+00:00

I want to create a char** character array on the stack. Currently, I’m using

  • 0

I want to create a char** character array on the stack. Currently, I’m using this, but I wonder if there is a nicer way:

char* buf[4];
char temp0[1024], temp1[1024], temp2[1024], temp3[1024];
buf[0] = temp0;
buf[1] = temp1;
buf[2] = temp2;
buf[3] = temp3;

EDIT: To be more clear, I can not simply use char buf[4][1024]. A function expecting an array of char pointers would crash, as it’s a fundamentally different data type. This is how I would create the array on the heap:

char** buf = malloc(sizeof(char*) * 4);
for(int i = 0; i < 4; i++)
{
    buf[i] = malloc(sizeof(char) * 1024);
}
  • 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-17T03:13:54+00:00Added an answer on June 17, 2026 at 3:13 am

    The solutions posted so far are both OK for 4 elements; for 10 they’re clunky, and for 100 not going to fly. I think this can scale better:

    enum { MAX_ROWS = 10, ROW_SIZE = 1024 };
    char bigbuffer[MAX_ROWS][ROW_SIZE];
    char *buf[MAX_ROWS];
    
    for (int i = 0; i < MAX_ROWS; i++)
        buf[i] = bigbuffer[i];
    
    ...and off you go...
    

    With C99 or later, you can parameterize the array size by using VLAs (variable length arrays):

    void buffer_creation(int rows, int cols)
    {
        char bigbuffer[rows][cols];
        char *buf[rows];
    
        for (int i = 0; i < rows; i++)
            buf[i] = bigbuffer[i];
    
        ...and off you go...
    }
    

    If the size gets too large, you can use malloc() instead, of course, but you have to make sure you free the space too:

    void buffer_creation(int rows, int cols)
    {
        char *buf[rows];  // Probably won't stress the stack ever
        char *bigbuffer = malloc(rows * cols);
        if (bigbuffer != 0)
        {    
            for (int i = 0; i < rows; i++)
                buf[i] = &bigbuffer[i * cols];
    
            ...and off you go...
            free(bigbuffer);
        }
    }
    

    Obviously, you could allocate the buf array too if you really want to — I’m leaving it as an exercise for the reader.

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

Sidebar

Related Questions

I want to create a new array with the same size of chaine but
I want to create a 2nd array using a function; the user will enter
I want to create the equivalent of: char* val = \x11\x11\x11\x11; using the htole32()
i want create Dynamic Slideshow in Jquery i'm Write this code var ctx =
i want to create a simple program that i have a array of strings
I want to create a simple menu in C program that accepts single character.
I want to create a Multicolumn expression index, but when I create the index,
In R, I want to create a factor with only a few levels, but
How to create an external character array in C? I have tried various ways
If I want to dynamically create an int array in C++ and then delete

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.