For resizable arrays you can use malloc()
and realloc()
. These allow you to reserve (with malloc()
) and resize (with realloc()
) a certain amount of space on the heap. They’re used this way:
int* a = malloc(10 * sizeof(int)); if(a == NULL) {} // malloc() was unable to allocate the memory, handle the // error and DO NOT use this pointer anymore // now you can treat a as a normal array of 10 ints: a[4] = 51; // suppose 10 ints aren't no more enough: a = realloc(a, 20 * sizeof(int)); if(a == NULL) {} // same thing as before // here you have 20 ints, the previous 10 are still there a[18] = a[4] // don't forget to free the memory when you have finished: free(a);
Just replace ‘int’ with your struct type. 😉
Related Posts:
- How to initialize array to 0 in C?
- How to do scanf for single char in C
- Sign extend a nine-bit number in C
- Break statement not within loop or switch in C
- How to properly malloc for array of struct in C
- Cache Simulator in C
- Where is the C auto keyword used?
- make: Nothing to be done for `all’
- expected expression before ‘{‘ token
- Setting std=c99 flag in GCC
- Convert char array to a int number in C
- Bind failed: Address already in use
- What is the difference between %f and %lf in C?
- What is stdin in C language?
- How to pass 2D array (matrix) in a function in C?
- error: ‘for’ loop initial declarations are only allowed in C99 mode [duplicate]
- c – warning: implicit declaration of function ‘printf’
- How to compile makefile using MinGW?
- Mapping a numeric range onto another
- Why are hexadecimal numbers prefixed with 0x?
- Portable way to check if directory exists [Windows/Linux, C]
- too many arguments for format [-Wformat-extra-args]
- How to use EOF to run through a text file in C?
- Incorrect checksum for freed object on malloc
- How to copy a char array in C?
- Simple way to check if a string contains another string in C?
- What does “request for member ‘*******’ in something not a structure or union” mean?
- cast to pointer from integer of different size, pthread code
- Invalid type argument of unary ‘*’ (have ‘int’) Error in C
- Segmentation fault (core dumped) due to fgets – I think
- Why do I get “cast from pointer to integer of different size” error?
- What does “request for member ‘*******’ in something not a structure or union” mean?
- Unknown ending signal when using debugger gdb
- Valgrind: invalid read of size 4 -> sigsegv, works fine without valgrind and in visual studio
- How to convert integer to char in C?
- Where to find the complete definition of off_t type?
- struct has no member named
- printf not printing to screen
- Initializing array of structures
- Still Reachable Leak detected by Valgrind
- Assembly x86 – “leave” Instruction
- What is the difference between array and enum in C ?
- What is signed integer overflow?
- What does the term “empty loop” refer to exactly in C and C++?
- When to use const char * and when to use const char []
- What do numbers using 0x notation mean?
- What integer hash function are good that accepts an integer hash key?
- Can I create an Array of Char pointers in C?
- Proper way to empty a C-String
- Does stack grow upward or downward?
- Convert Little Endian to Big Endian
- malloc(): memory corruption
- Excess elements of scalar initializer for pointer to array of ints
- X86 assembly – Handling the IDIV instruction
- error C2371: ‘functionname’ redefinition: different basic types
- How does one represent the empty char?
- Pause screen at program completion in C
- How to write to a file using open() and printf()?
- note: previous implicit declaration of ‘point_forward’ was here
- Can I define a function inside a C structure?
- What exactly is meant by “de-referencing a NULL pointer”?
- Difference between fgets and fscanf?
- getc() vs fgetc() – What are the major differences?
- Can’t understand the working of getint() in C as per K&R
- Lua – Number to string behaviour
- Tokenizing strings in C
- Reading in double values with scanf in c
- Realloc Invalid Pointer in C
- What tools are there for functional programming in C?
- typedef fixed length array
- In C, what exactly happens when you pass a NULL pointer to strcmp()?
- “-bash: gcc: command not found” using cygwin when compiling c?
- How to format strings using printf() to get equal length in the output
- What do \t and \b do?
- Why and when to use static structures in C programming?
- Where does linux store my syslog?
- Example of waitpid() in use?
- Invalid write of size 1
- How to solve static declaration follows non-static declaration in GCC C code?
- realloc(): invalid next size when reallocating to make space for strcat on char *
- Can a function return two values?
- C: warning: excess elements in array initializer; near initialization for ‘xxx’ ; expects ‘char *’, but has type ‘int’
- write() to stdout and printf output not interleaved?
- How to know what the ‘errno’ means?
- Split string with multiple delimiters using strtok in C
- Printf was not declared in this scope
- warning: missing terminating ” character [enabled by default]
- How to free memory from char array in C
- Can I get Unix’s pthread.h to compile in Windows?
- Initializing 2D char array in C
- FFT in a single C-fileÂ
- Process exited with return value 3221225477
- gdb: No symbol “i” in current context
- How do I check if a string contains a certain character?
- malloc(sizeof(int)) vs malloc(sizeof(int *)) vs (int *)malloc(sizeof(int))
- %p Format specifier in c
- Level vs Edge Trigger Network Event Mechanisms
- “Nothing to be done for makefile” message
- Convert Char to String in C
- What is the difference between signed and unsigned int