libft
Loading...
Searching...
No Matches
Files | Functions
2D Array Utilities

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.
 

Detailed Description

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:

Function Documentation

◆ ft_create_matrix()

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.

Parameters
rowsNumber of rows in the 2D array.
colsNumber of columns in the 2D array.
Returns
A pointer to the allocated 2D array, or NULL on failure.
Note
The returned array must be freed using ft_free_matrix to prevent memory leaks.
See also
ft_free_matrix

◆ ft_free_matrix()

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.

Parameters
matrixA pointer to the 2D array to free.
rowsThe number of rows in the array.
Note
Passing a NULL array is safe and does nothing.
See also
ft_create_matrix

◆ ft_matrix_copy()

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.

Parameters
matrixThe original 2D array to copy.
rowsThe number of rows in the array.
colsThe number of columns in the array.
Returns
A newly allocated deep copy of the array, or NULL on error.
Note
Uses ft_create_matrix to allocate the new array.
See also
ft_create_matrix

◆ ft_print_matrix()

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.

Parameters
matrixA 2D array of integers.
rowsThe number of rows in the array.
colsThe number of columns in the array.
Returns
0 on success, or -1 if any write error occurs.
Note
If the array is NULL or any ft_printf fails, returns -1.
See also
ft_printf

◆ ft_strarr_to_matrix()

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.).

Parameters
arrayA NULL-terminated array of strings (char **), where each string must contain only digit characters (0–9).
Returns
A newly allocated 2D integer array, or NULL on failure.
Note
Each string must have the same length. If allocation fails or the input is invalid, returns NULL.
See also
ft_create_matrix
ft_arraysize
ft_free_matrix