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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:08:48+00:00 2026-05-27T08:08:48+00:00

I have a char array char *data[]= {11, 22, 33, 44, 55}; How can

  • 0

I have a char array

char *data[]= {"11", "22", "33", "44", "55"};

How can I add some extra items to it in the end? data[]="66";

I’d like a dynamic array in C.

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-05-27T08:08:49+00:00Added an answer on May 27, 2026 at 8:08 am

    Arrays created using the [] syntax are not dynamic, the length is set at compile-time and cannot change.

    UPDATE: Actually, C99 adds so-called “variable-length arrays”, which can get their length at run-time. After they’ve been initialized, however, they can’t shrink or expand so the below still applies.

    However, an array is trivially expressed when you have pointers: an array can be represented as a pointer to the first element, and a length.

    So, you can create a new array by dynamically allocating memory using malloc():

    size_t array_length = 3;
    int *array = malloc(array_length * sizeof *array);
    
    if(array != NULL)
    {
      array[0] = 11;
      array[1] = 22;
      array[2] = 33;
    }
    

    You cannot use the {} list of elements here, that’s only usable when initializing arrays declared using the [] syntax.

    To grow the array, you can use the realloc() function to re-allocate the memory and copy the old values over:

    size_t new_length = array_length + 1;
    int *bigger_array = realloc(array, new_length * sizeof *bigger_array);
    
    if(bigger_array != NULL)
    {
      bigger_array[new_length - 1] = 44;
      /* We have successfully grown the allocation, remember the new address. */
      array = bigger_array;
      array_length = new_length;
    }
    

    Note that every time you call malloc() (or realloc()), it can return NULL if it failed to allocate the requested block. That’s why the if statements are needed. I cut the initial size down a bit from your example to reduce the number of assignment-lines needed, to make the example shorter.

    To make the above more efficient, typical dynamical array code uses two length values: one for the actual array (how many values are in the array right now) and one for the memory (how many values to we have room to store). By making the latter value grow in chunks, the total number of memory allocations can be cut down a bit, at the cost of some memory of course.

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

Sidebar

Related Questions

Have an array of chars like char members[255]. How can I empty it completely
if I have an array of pointers like char **lines, how can i determine
I have a big lump of binary data in a char[] array which I
I have taken char data into database into array. now i want to convert
I currently have the dynamic array: char *myData[500][10]; //myData is the name of an
I have a 2 dimensional array, like so: char[,] str = new char[2,50]; Now,
I have some doubts in basic C programming. I have a char array and
I have this array: unsigned char* data = CGBitmapContextGetData(cgctx); then I tried to get
I have a char array in a C application that I have to split
I have a char array buffer that I am using to store characters that

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.