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

Functions for working with pointer arrays (char**, void**, etc.). More...

Files

file  ft_array.c
 Implementation of pointer and integer array utilities.
 
file  ft_array.h
 Utility functions for handling pointer and integer arrays.
 

Functions

size_t ft_arraysize (void **array)
 Returns the number of elements in a NULL-terminated array.
 
char ** ft_copy_strarray (char **array)
 Creates a deep copy of a NULL-terminated string array.
 
void ft_free_array (void **array)
 Frees a NULL-terminated array.
 
void ft_free_array_size (void **array, size_t size)
 Frees an array of pointers with a known size.
 
bool ft_is_array_sorted (const int *array, size_t size)
 Checks if an array of integers is sorted in ascending order.
 
int ft_putintarray (int *array, int size)
 Prints an array of integers to standard output.
 

Detailed Description

Functions for working with pointer arrays (char**, void**, etc.).

Includes functions for:

This group includes:

See also
ft_arraysize
ft_copy_strarray
ft_free_array
ft_free_array_size
ft_is_array_sorted
ft_putintarray

Function Documentation

◆ ft_arraysize()

size_t ft_arraysize ( void **  array)

#include <include/ft_array.h>

Returns the number of elements in a NULL-terminated array.

Calculates the number of elements in a NULL-terminated array.

Parameters
arrayThe array of pointers (NULL-terminated).
Returns
The number of elements in the array.

This function iterates over a pointer array (usually a char ** or similar) and returns the number of non-NULL elements until it hits the terminating NULL.

Parameters
arrayA NULL-terminated array of pointers.
Returns
The number of elements before the NULL terminator. Returns 0 if the input array is NULL.
Note
This is commonly used to get the size of a string array like the result of ft_split, or an environment variable array.

◆ ft_copy_strarray()

char ** ft_copy_strarray ( char **  array)

#include <include/ft_array.h>

Creates a deep copy of a NULL-terminated string array.

Parameters
arrayThe original string array to copy.
Returns
A newly allocated copy of the array, or NULL on failure.

Allocates a new array of strings and duplicates each string from the input array using ft_strdup. If any allocation fails, all previously allocated strings are freed and NULL is returned.

Parameters
arrayA NULL-terminated array of strings (char **).
Returns
A newly allocated copy of the array with duplicated strings, or NULL if allocation fails or if the input array is NULL.
Note
The returned array must be freed using a function like ft_free_array to avoid memory leaks.
See also
ft_strdup
ft_free_array
ft_arraysize

◆ ft_free_array()

void ft_free_array ( void **  array)

#include <include/ft_array.h>

Frees a NULL-terminated array.

Frees a NULL-terminated array of pointers.

Parameters
arrayThe array to free.

Iterates through the array and frees each non-NULL element, then frees the array itself.

Parameters
arrayA NULL-terminated array of dynamically allocated pointers.
Note
Safe to call with NULL. Use after functions like ft_split or ft_copy_strarray to release memory.

◆ ft_free_array_size()

void ft_free_array_size ( void **  array,
size_t  size 
)

#include <include/ft_array.h>

Frees an array of pointers with a known size.

Frees a pointer array of known size.

Parameters
arrayThe array to free.
sizeNumber of elements in the array.

Frees each non-NULL element of the array up to the given size, then frees the array itself.

Parameters
arrayAn array of dynamically allocated pointers.
sizeThe number of elements to free (not including a terminator).
Note
Useful when the array is not NULL-terminated or its size is known ahead of time.

◆ ft_is_array_sorted()

bool ft_is_array_sorted ( const int *  array,
size_t  size 
)

#include <include/ft_array.h>

Checks if an array of integers is sorted in ascending order.

Checks if an integer array is sorted in ascending order.

Parameters
arrayThe array to check.
sizeNumber of elements in the array.
Returns
true if sorted, false otherwise.
Parameters
arrayThe array to check.
sizeThe number of elements in the array.
Returns
true if the array is sorted, false otherwise.

◆ ft_putintarray()

int ft_putintarray ( int *  array,
int  size 
)

#include <include/ft_array.h>

Prints an array of integers to standard output.

Prints an integer array to standard output.

Parameters
arrayThe array to print.
sizeNumber of elements.
Returns
The number of characters printed, or -1 on error.

Each integer is printed using ft_printf followed by a space, except the last element. The function returns the total number of characters printed.

Parameters
arrayA pointer to an array of integers.
sizeThe number of elements in the array.
Returns
The total number of characters printed on success, or -1 if ft_printf fails.
Note
If array is NULL or size <= 0, the function returns 0.