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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:26:50+00:00 2026-05-12T17:26:50+00:00

I am trying to allocate a array of char*’s in C. I know the

  • 0

I am trying to allocate a array of char*’s in C.
I know the number of columns in advance, but not the rows
and I want to allocate the rows as and when needed.

I tried to use:

char *(*data)[NUMCOLS]; //declare data as pointer to array NUMCOLS of pointer to char

data = malloc(sizeof(char*));

now, the above line should allocate for data[0] … correct?
then, I must be able to use the row like

data[0][1] = strdup("test");
 .
 ..
data[0][NUMCOLS-1] = strdup("temp");

I am getting seg fault. I am not able to understand what is wrong here.
can anyone please help.

  • 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-12T17:26:51+00:00Added an answer on May 12, 2026 at 5:26 pm

    I would do this:

    #include <stdlib.h> 
    #include <stdio.h> 
    #include <string.h> 
    
    int main(){
      char ***a = NULL;
    
      a       = realloc( a, 1 * sizeof(char **) ); // resizing the array to contains one raw
      a[0]    = malloc(     3 * sizeof(char  *) ); // the new raw will contains 3 element
      a[0][0] = strdup("a[0][0]");
      a[0][1] = strdup("a[0][1]");
      a[0][2] = strdup("a[0][2]");
    
    
      a       = realloc( a, 2 * sizeof(char **) ); // resizing the array to contains two raw
      a[1]    = malloc(     3 * sizeof(char  *) ); // the new raw will contains 3 element
      a[1][0] = strdup("a[1][0]");
      a[1][1] = strdup("a[1][1]");
      a[1][2] = strdup("a[1][2]");
    
      for( int rows=0; rows<2; rows++ ){
        for( int cols=0; cols<3; cols++ ){
          printf( "a[%i][%i]: '%s'\n", rows, cols, a[rows][cols] );
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to allocate memory for a multidimensional array (8 rows, 3 columns). Here's
I am trying to allocate a 2D dimension array of File Descriptors... So I
I am trying to allocate a dynamic string array in the following way and
I'm trying to create a function that would dynamically allocate an array, sets the
This question is in C++. I am trying to dynamically allocate an array of
I am trying to allocate memory for an array of unknown size that is
Trying to print out the bits of each char stored in an array. I've
I'm trying to allocate a 2d array in a C program. It works fine
i am trying to allocate memory for a 2 dimensional array using c and
I am working in VC++ 2008, and am trying to allocate a multi-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.