No, you can't change the size of an array. You could use a dynamically allocated list of char* instead and realloc() as required:

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

int main() {
    char** array = malloc(1 * sizeof(*array));

    if (array) {
        array[0] = "This";

        printf("%s\n------\n", array[0]);

        char** tmp = realloc(array, 2 * sizeof(*array));
        if (tmp) {
            array = tmp;
            array[1] = "That";

            printf("%s\n", array[0]);
            printf("%s\n", array[1]);
        }

        free(array);
    }
    return 0;
}

See online demo: https://ideone.com/ng00k.

Answer from hmjd on Stack Overflow
🌐
Reddit
reddit.com › r/cprogramming › resizing an array and returning it?
r/cprogramming on Reddit: Resizing an array and returning it?
January 30, 2023 -

So I said that 2023 I would learn C. I took a computer science course a while ago and they thought C at the start. I enjoyed it but did not stick with C, now I am back (atleast trying). Please grill me if necessary.

So I am messing around with arrays and malloc. I want to write a program that resizes arrays and pushes numbers into them.

My question is, when returning the newly size array, should I return the address of the array struct or just the array struct itself return arr VS return &arr. OR am i totally wrong? Would appreciate any feedback.

My thought process behind this,

define struct that has an array of size 1 and a length property

initialize this struct in main using malloc

ask the user what number to 'push' into the array

call function that create a new struct using malloc, 
copies values from old array into it and increment length property and returns the new struct. 
This function also frees the old struct.

call function to push new number into newly created array struct

repeat

free last created struct at the end of the program.

main:

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

typedef struct int_array
{
    int length;
    int array[1];
} int_array;

int main(void)
{
    // Initialize array struct
    int_array *arr_struct = malloc(sizeof(int_array));

    if (arr_struct == NULL)
    {
        printf("In attempting to create the intial array, an error occured\n");
        return 1;
    }

    arr_struct->length = 1;
     
    for (int i = 0; i < arr_struct->length; i++)
    {
        arr_struct->array[i] = 10;
    }

    // While loop will go here

    free(arr_struct);
    return 0;

}

size up function

int * size_up(int_array *arr)
{
    int old_length = arr->length;

    int_array *new_arr = malloc(sizeof(int_array) + (old_length + 1));

    new_arr->length = old_length+1;

    // Copy values from old array to new array

    for (int i = 0; i < old_length; i++)
    {
        new_arr->array[i] = arr->array[i];
    }

    // Free old array struct
    
    free(arr);

    return &new_arr;
}

push

void int_array_push(int_array *arr, int num_to_push)
{
    int last_index = arr->length - 1;

    arr->array[last_index] = num_to_push;
}
Top answer
1 of 6
5
return arr is the same as return &arr, because an array "decays" to a pointer already. Idiomatically, "return arr;" would be preferred. Also, you might want to look into realloc() .
2 of 6
3
If you want to create your struct like that, I'd do something like this: #include // ... int array_length = 10; int_array *new_arr; new_arr = malloc(offsetof(int_array, array) + array_length * sizeof(new_arr.array); new_arr->length = array_length; This uses the offsetof macro to calculate the start position of the array data, then addes the necessary space to store the array beginning at that start position. What you'll often see instead is this pattern: struct growable_array { int capacity; int length; int *contents; }; void create_growable_array(int initial_capacity) { struct growable_array *a; a = malloc(sizeof(*a)); if (a == NULL) { /* handle OOM */ } a->capacity = initial_capacity; a->length = 0; a->contents = malloc(initial_capacity * sizeof(a->contents)); if (a->contents == NULL) { /* handle OOM */ } return a; } void append_growable_array(struct growable_array *a, int value_to_append) { assert(a != NULL); if (a->length >= a->capacity) { a->capacity *= 2; a = realloc(a->capacity * sizeof(a->contents)); if (a == NULL) { /* handle OOM */ } } a->contents[a->length++] = value_to_append; } int get_growable_array(struct growable_array *a, int index) { assert(a != NULL); assert(index >= 0); assert(index < a->length); return a->contents[index]; } void set_growable_array(struct growable_array *a, int index, int value_to_set) { assert(a != NULL); assert(index >= 0); assert(index < a->length); a->contents[index] = value_to_set; }
Discussions

Dynamically resizing an array - C++ Forum
I have an assignment involving functions and pointers. It's better to just paste what I am supposed to do: a. Create a function called resize that can be used to increase the size of integer arrays dynamically. The function takes three parameters. The first parameter is the original array, ... More on cplusplus.com
🌐 cplusplus.com
April 17, 2018
How to resize c# array of gameobjects& - Questions & Answers - Unity Discussions
I have scripts using “c# array”, so replacing evrything to “c# list” will be a headache. Help with code ------ if(MyUnits[MyUnits.length] != null){ Resize(MyUnits, (MyUnits.length) + 10); } ------ public void Resize(GameObjectsArray, NewSize){ ///WHAT GOES HERE???? } More on discussions.unity.com
🌐 discussions.unity.com
0
March 16, 2014
Resizing arrays
This post is not exactly for the Atmel hardware but for an ARM MCU from TI and is being programmed using the Energia IDE. However Energia is based on the Arduino IDE and has the exact same interface and syntax. If it works in Arduino, it'll work in Energia (well, almost). More on forum.arduino.cc
🌐 forum.arduino.cc
0
0
June 14, 2016
Expanding the size of an array in C? - General and Gameplay Programming | GameDev.net Forums - GameDev.net
Arrays are continuous in memory, so can't be resized. Arrays however have O(1) access time, so this is the advantage of that. Vectors are resizable, but are slower than arrays. If you have to resize your array, the only way to do it is create a new array of the new size, then copy over all ... More on gamedev.net
🌐 gamedev.net
October 18, 2006
🌐
GeeksforGeeks
geeksforgeeks.org › c language › dynamic-array-in-c
Dynamic Array in C - GeeksforGeeks
July 23, 2025 - Array in C is static in nature, so its size should be known at compile time and we can't change the size of the array after its declaration. Due to this, we may encounter situations where our array doesn't have enough space left for required ...
🌐
Quora
quora.com › How-can-we-resize-an-array-in-C-C
How can we resize an array in C/C++? - Quora
Arrays, for definition, have a fixed length. If you need resize an array, you need a dynamic array. In this case, the array is created with a size and that size can be increased by a factor. Some languages have dynamic arrays in their syntax. C and C++ hasn’t. In the C++...
🌐
LabEx
labex.io › tutorials › c-how-to-resize-arrays-safely-in-c-464811
How to resize arrays safely in C | LabEx
Learn efficient techniques for dynamically resizing arrays in C programming, covering memory management, reallocation strategies, and safe memory handling for optimal performance.
🌐
Cplusplus
cplusplus.com › forum › beginner › 235218
Dynamically resizing an array - C++ Forum
April 17, 2018 - I have an assignment involving functions and pointers. It's better to just paste what I am supposed to do: a. Create a function called resize that can be used to increase the size of integer arrays dynamically. The function takes three parameters. The first parameter is the original array, ...
Find elsewhere
🌐
W3Schools
w3schools.com › c › c_arrays_size.php
C Get the Size of an Array
You learned from the Data Types chapter that an int type is usually 4 bytes, so from the example above, 4 x 5 (4 bytes x 5 elements) = 20 bytes. Knowing the memory size of an array is great when you are working with larger programs that require good memory management.
🌐
NeetCode
neetcode.io › problems › dynamicArray
NeetCode
A better way to prepare for coding interviews.
🌐
Unity
discussions.unity.com › questions & answers
How to resize c# array of gameobjects& - Questions & Answers - Unity Discussions
March 16, 2014 - I have scripts using “c# array”, so replacing evrything to “c# list” will be a headache. Help with code ------ if(MyUnits[MyUnits.length] != null){ Resize(MyUnits, (MyUnits.length) + 10); } ------ public void Resize(Ga…
🌐
C# Corner
c-sharpcorner.com › article › how-to-resize-an-array-in-c-sharp
How To Resize An Array In C#
February 7, 2023 - This article will discuss how to resize an Array In C#. The Array class in the System namespace provides the Resize() method that can be used to update the size of an existing array in C#.
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Resizing arrays - Programming - Arduino Forum
June 14, 2016 - This post is not exactly for the Atmel hardware but for an ARM MCU from TI and is being programmed using the Energia IDE. However Energia is based on the Arduino IDE and has the exact same interface and syntax. If it wor…
🌐
D Programming Language
dlang.org › spec › arrays.html
Arrays - D Programming Language
If the new array length is shorter, ... data is copied. It is equivalent to slicing the array: ... If the new array length is longer, the array is reallocated if necessary, preserving the existing elements. The new elements are filled out with the default initializer. To maximize efficiency, the runtime always tries to resize the array ...
🌐
Wikipedia
en.wikipedia.org › wiki › Dynamic_array
Dynamic array - Wikipedia
1 month ago - Elements can be removed from the end of a dynamic array in constant time, as no resizing is required. The number of elements used by the dynamic array contents is its logical size or length, while the size of the underlying array is called the dynamic array's physical size or capacity.
🌐
DEV Community
dev.to › crazysamurai › how-to-increase-the-size-of-an-array-in-c-50o0
How To Increase The Size Of An Array In C ? - DEV Community
August 29, 2021 - Steps to get the job done: 1.Create an array (lets say p) with n items. 2.Create another array (lets say q) but an empty one which is larger than array p. 3.Now copy the elements of p into q by a simple for loop. 4.Delete the memory held by pointer p using free(p); so that array p no longer exists.
🌐
C# Corner
c-sharpcorner.com › article › resizing-arrays-in-c-sharp-with-examples
Resizing Arrays in C# with Examples
May 28, 2024 - In C#, you can resize an array using the Array.Resize<T> method. This method allows you to change the number of elements in a one-dimensional array to a specified new size.
🌐
GameDev.net
gamedev.net › home › forums › programming › general and gameplay programming › expanding the size of an array in c?
Expanding the size of an array in C? - General and Gameplay Programming | GameDev.net Forums - GameDev.net
October 18, 2006 - You can't resize an array in C, their size is set at compile time. You can have a dynamically-sized array-like memory region, but you have to use memory allocation. Master it and move on. Don't let the technology master you.
🌐
YouTube
youtube.com › watch
How to resize an array in C (Part 1) - YouTube
Source code can be found here:https://code-vault.net/lesson/a985b2dd74cfa127eec967874e001035===== Support us through our store =====https://code-vault.net/sh...
Published   May 22, 2024
🌐
YouTube
youtube.com › watch
How to Resize an Array in C/C++? Is there a best way? - YouTube
Patreon ➤ https://www.patreon.com/jacobsorberCourses ➤ https://jacobsorber.thinkific.comWebsite ➤ https://www.jacobsorber.com---***Welcome! I post videos tha...
Published   November 15, 2022
🌐
YouTube
youtube.com › watch
How To Resize An Array | C Programming Tutorial - YouTube
How to resize an array using C. Examples of using realloc() to resize a dynamically allocated array are demonstrated. And also discussed is the workaround ...
Published   July 9, 2024