Skip to content

Airbus5717/Arraylist

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

Array Macros for Dynamic Arrays

A set of macros for managing dynamic arrays in C. These macros simplify operations like creation, resizing, and memory management.


Declaring the Array Type

Before using the macros, you need to declare the array type globally using the generate_array_type(type) macro. This should be placed in the global header scope to define the array type structure for the specified type type.

generate_array_type(int);

This declares a dynamic array type for int. You can use any valid C type, such as float, double, or custom structs.


Macros

array_make(T, size)

Creates a dynamic array of type T with an initial size.

Usage:

Array(int) myArray = array_make(int, 10);

array_free(arr)

Frees the memory of the array.

Usage:

array_free(myArray);

array_push(arr, value)

Appends a value to the end of the array, resizing if necessary.

Usage:

array_push(myArray, 42);

array_start(arr)

Returns a pointer to the first element of the array.

Usage:

int *start = array_start(myArray);

array_end(arr)

Returns a pointer to the last element of the array.

Usage:

int *end = array_end(myArray);

array_at(arr, idx)

Accesses the element at a specific index.

Usage:

int value = array_at(myArray, 2);

array_length(arr)

Returns the number of elements in the array.

Usage:

usize length = array_length(myArray);

array_for_each(arr, el)

Iterates over each element in the array.

Usage:

array_for_each(myArray, el)
{
    printf("%d\n", *el);
}

Example

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

// Assuming macros are included

// Declare the array type globally
generate_array_type(int);

int main()
{
    Array(int) myArray = array_make(int, 5); // Create array

    array_push(myArray, 10); // Add values
    array_push(myArray, 20);
    array_push(myArray, 30);

    array_for_each(myArray, el) // Print values
    {
        printf("%d\n", *el);
    }

    array_free(myArray); // Free memory
    return 0;
}

Notes

  • Ensure sufficient memory when resizing arrays.
  • Handle memory allocation failures with error handling.
  • Always free arrays with array_free to prevent memory leaks.

About

Arraylist for C

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages