|
libft
|
Utilities for working with 2D integer arrays. More...
Files | |
| file | ft_2darray.c |
| Utilities for dynamically allocated 2D integer arrays. | |
| file | ft_2darray.h |
| 2D integer array utilities. | |
Functions | |
| int ** | ft_create_matrix (size_t rows, size_t cols) |
| Allocates a 2D array of integers initialized to 0. | |
| void | ft_free_matrix (int **matrix, size_t rows) |
| Frees a 2D array of integers. | |
| int ** | ft_matrix_copy (int **matrix, size_t rows, size_t cols) |
| Creates a deep copy of a 2D array of integers. | |
| int | ft_print_matrix (int **matrix, size_t rows, size_t cols) |
| Prints a 2D array of integers to the standard output. | |
| int ** | ft_strarr_to_matrix (char **array) |
| Converts a NULL-terminated array of strings to a 2D integer array. | |
Utilities for working with 2D integer arrays.
These functions are designed to create, copy, convert, print, and free dynamically allocated 2D integer arrays (grids).
Useful for grid-based applications such as game maps or algorithmic tasks.
This group includes:
| int ** ft_create_matrix | ( | size_t | rows, |
| size_t | cols | ||
| ) |
#include <include/ft_2darray.h>
Allocates a 2D array of integers initialized to 0.
This function allocates a 2D array with the specified number of rows and columns, initializing all values to 0 using ft_calloc. If allocation fails at any point, all previously allocated memory is freed and NULL is returned.
| rows | Number of rows in the 2D array. |
| cols | Number of columns in the 2D array. |
ft_free_matrix to prevent memory leaks.| void ft_free_matrix | ( | int ** | matrix, |
| size_t | rows | ||
| ) |
#include <include/ft_2darray.h>
Frees a 2D array of integers.
Releases memory allocated for a 2D array created with ft_create_matrix or equivalent. Each row is freed individually, followed by the array of pointers.
| matrix | A pointer to the 2D array to free. |
| rows | The number of rows in the array. |
| int ** ft_matrix_copy | ( | int ** | matrix, |
| size_t | rows, | ||
| size_t | cols | ||
| ) |
#include <include/ft_2darray.h>
Creates a deep copy of a 2D array of integers.
Allocates and returns a new 2D array with the same dimensions and values as the input. This function is useful when you need to work with a copy of the original data without modifying it.
| matrix | The original 2D array to copy. |
| rows | The number of rows in the array. |
| cols | The number of columns in the array. |
ft_create_matrix to allocate the new array.| int ft_print_matrix | ( | int ** | matrix, |
| size_t | rows, | ||
| size_t | cols | ||
| ) |
#include <include/ft_2darray.h>
Prints a 2D array of integers to the standard output.
Uses ft_printf to print each row of the 2D array on a separate line. Each integer is followed by a space. If any print call fails, returns -1.
| matrix | A 2D array of integers. |
| rows | The number of rows in the array. |
| cols | The number of columns in the array. |
ft_printf fails, returns -1.| int ** ft_strarr_to_matrix | ( | char ** | array | ) |
#include <include/ft_2darray.h>
Converts a NULL-terminated array of strings to a 2D integer array.
Each string in the array is interpreted as a row of digit characters. Returns a 2D array of integers where each character is converted to its numeric value (‘'0’→ 0,'1'` → 1, etc.).
| array | A NULL-terminated array of strings (char **), where each string must contain only digit characters (0–9). |