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 OverflowNo, 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.
There is no way to resize an array. You can simply create a new array of size 2, then copy all the data from the previous one to the new one. realloc does it for you with dynamic memory. The better way is to use data structures such as LinkedLists or Vectors which you can find more about online.
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;
}