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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:09:24+00:00 2026-06-01T20:09:24+00:00

In C++ I can allocate an array of strings (say 10 strings) very easily

  • 0

In C++ I can allocate an array of strings (say 10 strings) very easily as:

string* arrayOfString = new string[10];

however I don’t know how to do it in ANSI C. I tried:

char** arrayOfString = (*(char[1000])) malloc (sizeof (*(char[1000])));

But the compiler (MinGW 3.4.5) keeps saying that it is “Syntax error”.
How to do it right?
Thanks.

  • 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-01T20:09:25+00:00Added an answer on June 1, 2026 at 8:09 pm

    If each string is of same size which is known at compile time, say it is 100, then you can do this1:

    typedef char cstring[100]; //cstring is a new type which is 
                               //a char array of size 100
    
    cstring *arrstring = malloc (1000 * sizeof (cstring));
    
    int i;
    for( i = 0 ; i < 1000 ; ++i)
         strcpy(arrstring[i], "some string of size less than or equal to 100");
    
    for( i = 0 ; i < 1000 ; ++i)
         printf("%s\n", arrstring[i]);
    

    Demo : http://ideone.com/oNA30

    1. Note that as @Eregrith pointed out in the comment that cast is not advised if you compile your code as C. However, if you compile it as C++, then you need to write (cstring*)malloc (1000 * sizeof (cstring)) but then in C++, you should avoid writing such code in the first place. A better alternative in C++ is, std::vector<std::string> as explained at the bottom of this post.

    If the size of each string is not known at compile time, or each string is not of same size, then you can do this:

    char **arrstring =  malloc(sizeof(char*) * 1000); //1000 strings!
    int i;
    for(i = 0 ; i < 1000; ++i)
         arrstring[i] = (char*) malloc(sizeof(char) * sizeOfString); 
    

    I’m assuming same size sizeOfString for all 1000 strings. If they’re different size, then you’ve to pass different value in each iteration, something like this:

    for(i = 0 ; i < 1000; ++i)
         arrstring[i] = malloc(sizeof(char) * sizeOfEachString[i]); 
    

    I hope that helps you, and I also hope you can do the rest yourself.


    By the way, in C++, you should not do this:

    string* arrayOfString = new string[10]; //avoid new as much as possible!
    

    Instead, you should do this:

    std::vector<std::string>  strings;
    strings.reserve(10);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When you allocate an array using new [] , why can't you find out
In C, I know I can dynamically allocate a two-dimensional array on the heap
can anyone help me on this.Lets say I have this array of string e.g.
I'm trying to allocate and initialize an array inside a function, but I can't
I have a server application that, in rare occasions, can allocate large chunks of
I allocate a 2d array and use memset to fill it with zeros. #include<stdio.h>
I am trying to seperate values in an array so i can pass them
I want to take a string from stdin but I don't want a static
I need an array which I can access from different methods, I have to
I'm attempting to create an array of strings that represent the directories stored in

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.