3

I have a char array of char array like so:

char my_test[2][10];

As you can see I have a length of 2 and then 10. If I need to increase the first char array (2), how can this be done dynamically?

For example, half way through my application char[2] might be in use so therefore I need to use position 3 in the char array. I would then end up with this:

char store[3][10];

But keeping the data originally store in:

char store[0][10];
char store[1][10];
char store[2][10];
6
  • Named arrays cannot be resized in C. You will need to use the malloc and realloc functions to create a resizeable array. Commented Nov 1, 2017 at 21:04
  • 1
    In C, "I need to increase the first char array", is a false premise. Instead, create a pointer to data and later increase the memory's allocation with realloc(). Commented Nov 1, 2017 at 21:11
  • 1
    "As you can see I have a length of 2 and then 10." -- NO! You have an array of 2 10 character arrays. A 2D array is an array of arrays. Here, you have and array of 2 - 10-char arrays. Arrays cannot be resized. Blocks of memory can. You can declare char (*my_test)[10]; to create a pointer to an array of 10-chars and then allocate/reallocate the number of 10-char arrays in that block. Or, you can decalre char **my_test; and allocate and reallocate each individual block each pointer points to. Commented Nov 1, 2017 at 21:31
  • Ok. I did say I'm a beginner and learning C, I have read about pointers but they confuse the hell out of me. Does anyone have a really newbie way of explaining them? Commented Nov 1, 2017 at 21:35
  • 1
    Pointers are simply variables that hold the address of something else as their value. (a pointer points to where in memory a value is found) So where int a = 5; (a holds the immediate value 5 as its value), int *b; declares a pointer to type int. Meaning it will hold the address of where an integer is stored as its value. (e.g. b = &a;) would assign the address where a is stored to the pointer b). Nothing else special about them. Commented Nov 1, 2017 at 21:38

2 Answers 2

3

You should dynamically allocate memory for the array using standard C functions malloc and realloc declared in header <stdlib.h>.

Here is a demonstrative program that shows how the memory can be allocated.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N   10

int main(void) 
{
    size_t n = 2;
    char ( *my_test )[N] = malloc( n * sizeof( char[N] ) );

    strcpy( my_test[0], "first" );
    strcpy( my_test[1], "second" );

    for ( size_t i = 0; i < n; i++ ) puts( my_test[i] );

    putchar( '\n' );

    char ( *tmp )[N] = realloc( my_test, ( n + 1 ) * sizeof( char[N] ) );

    if ( tmp != NULL )
    {
        my_test = tmp;
        strcpy( my_test[n++], "third" );
    }

    for ( size_t i = 0; i < n; i++ ) puts( my_test[i] );

    free( my_test );

    return 0;
}

The program output is

first
second

first
second
third
Sign up to request clarification or add additional context in comments.

Comments

2
char my_test[2][10];

is compile-time constant, which means that the required memory to use that array is carved to the stone already before your application starts. So you will never be able to change it's size.

You'll have to use DYNAMIC allocation. Check for something called malloc and free in case you are really working with C, or with C++ new and delete are what you need. You'll also need to learn about pointers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.