42 • C • QUICK REFERENCE

La référence C
pour la 42.

Une documentation rapide pour retrouver une fonction, comprendre sa logique et revoir les bases sans chercher dans plusieurs fichiers.

01 — Strings

Une fonction = une fiche indépendante.

ft_strlen

STRING
Prototypeint ft_strlen(char *str)
IdéeCompter jusqu'à '\0'
int	ft_strlen(char *str)
{
	int	i;

	i = 0;
	while (str[i])
		i++;
	return (i);
}

ft_strcpy

STRING
Prototypechar *ft_strcpy(char *dest, char *src)
IdéeCopier puis '\0'
char	*ft_strcpy(char *dest, char *src)
{
	int	i;

	i = 0;
	while (src[i])
	{
		dest[i] = src[i];
		i++;
	}
	dest[i] = '\0';
	return (dest);
}

ft_strcmp

STRING
IdéeComparer caractère par caractère
Retours1[i] - s2[i]
int	ft_strcmp(char *s1, char *s2)
{
	int	i;

	i = 0;
	while (s1[i] && s1[i] == s2[i])
		i++;
	return (s1[i] - s2[i]);
}

ft_strdup

MALLOC
Logiquestrlen → malloc → copie
Réflexe+1 pour '\0'
char	*ft_strdup(char *src)
{
	char	*dup;
	int		i;

	dup = malloc(sizeof(char) * (ft_strlen(src) + 1));
	if (!dup)
		return (NULL);
	i = 0;
	while (src[i])
	{
		dup[i] = src[i];
		i++;
	}
	dup[i] = '\0';
	return (dup);
}

ft_strchr

STRING
IdéeChercher un caractère
RetourAdresse du caractère ou NULL
char	*ft_strchr(char *str, char c)
{
	int	i;

	i = 0;
	while (str[i])
	{
		if (str[i] == c)
			return (&str[i]);
		i++;
	}
	if (c == '\0')
		return (&str[i]);
	return (NULL);
}

ft_strjoin

STRING
LogiqueDeux longueurs → malloc → deux copies
AttentionTerminer par '\0'
char	*ft_strjoin(char *s1, char *s2)
{
	char	*str;
	int		i;
	int		j;

	str = malloc(sizeof(char) * (ft_strlen(s1) + ft_strlen(s2) + 1));
	if (!str)
		return (NULL);
	i = 0;
	while (s1[i])
	{
		str[i] = s1[i];
		i++;
	}
	j = 0;
	while (s2[j])
	{
		str[i + j] = s2[j];
		j++;
	}
	str[i + j] = '\0';
	return (str);
}

02 — Memory

Allocation et manipulation de mémoire.

malloc

MEMORY
RéflexeVérifier le retour
Cyclemalloc → utilisation → free
char	*str;

str = malloc(sizeof(char) * 10);
if (!str)
	return ;
/* utilisation */
free(str);

ft_memset

MEMORY
void	*ft_memset(void *ptr, int value, size_t size)
{
	size_t	i;
	unsigned char	*p;

	p = ptr;
	i = 0;
	while (i < size)
	{
		p[i] = (unsigned char)value;
		i++;
	}
	return (ptr);
}

ft_memcpy

MEMORY
void	*ft_memcpy(void *dst, void *src, size_t n)
{
	size_t	i;
	unsigned char	*d;
	unsigned char	*s;

	d = dst;
	s = src;
	i = 0;
	while (i < n)
	{
		d[i] = s[i];
		i++;
	}
	return (dst);
}

03 — Characters

Tests et transformations de caractères.

ft_isalpha

CHAR
int	ft_isalpha(char c)
{
	return ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'));
}

ft_isdigit

CHAR
int	ft_isdigit(char c)
{
	return (c >= '0' && c <= '9');
}

ft_isalnum

CHAR
int	ft_isalnum(char c)
{
	return (ft_isalpha(c) || ft_isdigit(c));
}

ft_isascii

CHAR
int	ft_isascii(char c)
{
	return (c >= 0 && c <= 127);
}

ft_isprint

CHAR
int	ft_isprint(char c)
{
	return (c >= 32 && c <= 126);
}

ft_toupper

CHAR
char	ft_toupper(char c)
{
	if (c >= 'a' && c <= 'z')
		return (c - 32);
	return (c);
}

ft_tolower

CHAR
char	ft_tolower(char c)
{
	if (c >= 'A' && c <= 'Z')
		return (c + 32);
	return (c);
}

04 — Numbers & Arrays

Les petits algorithmes classiques d'évaluation.

ft_swap

POINTER
void	ft_swap(int *a, int *b)
{
	int	tmp;

	tmp = *a;
	*a = *b;
	*b = tmp;
}

ft_rev_int_tab

ARRAY
void	ft_rev_int_tab(int *tab, int size)
{
	int	i;
	int	tmp;

	i = 0;
	while (i < size / 2)
	{
		tmp = tab[i];
		tab[i] = tab[size - 1 - i];
		tab[size - 1 - i] = tmp;
		i++;
	}
}

ft_sort_int_tab

ARRAY
void	ft_sort_int_tab(int *tab, int size)
{
	int	i;
	int	j;
	int	tmp;

	i = 0;
	while (i < size - 1)
	{
		j = i + 1;
		while (j < size)
		{
			if (tab[i] > tab[j])
			{
				tmp = tab[i];
				tab[i] = tab[j];
				tab[j] = tmp;
			}
			j++;
		}
		i++;
	}
}

ft_iterative_power

MATH
int	ft_iterative_power(int nb, int power)
{
	int	result;

	if (power < 0)
		return (0);
	result = 1;
	while (power--)
		result *= nb;
	return (result);
}

ft_recursive_power

MATH
int	ft_recursive_power(int nb, int power)
{
	if (power < 0)
		return (0);
	if (power == 0)
		return (1);
	return (nb * ft_recursive_power(nb, power - 1));
}

ft_range

ARRAY
int	*ft_range(int min, int max)
{
	int	*tab;
	int	i;

	if (min >= max)
		return (NULL);
	tab = malloc(sizeof(int) * (max - min));
	if (!tab)
		return (NULL);
	i = 0;
	while (min < max)
	{
		tab[i] = min;
		i++;
		min++;
	}
	return (tab);
}

05 — I/O

Les bases de read, write et des fichiers.

ft_putchar

WRITE
void	ft_putchar(char c)
{
	write(1, &c, 1);
}

ft_putstr

WRITE
void	ft_putstr(char *str)
{
	int	i;

	i = 0;
	while (str[i])
	{
		write(1, &str[i], 1);
		i++;
	}
}

ft_putnbr

WRITE
void	ft_putnbr(int nb)
{
	char	c;

	if (nb == -2147483648)
	{
		write(1, "-2147483648", 11);
		return ;
	}
	if (nb < 0)
	{
		write(1, "-", 1);
		nb = -nb;
	}
	if (nb >= 10)
		ft_putnbr(nb / 10);
	c = nb % 10 + '0';
	write(1, &c, 1);
}

read

I/O
char	buf[1024];
int	ret;

ret = read(fd, buf, 1024);
if (ret > 0)
	write(1, buf, ret);

open / close

I/O
fd = open(file, O_RDONLY);
if (fd < 0)
	return ;
/* utilisation */
close(fd);

06 — Parsing

Découper et interpréter les chaînes.

ft_atoi

À CONNAÎTRE
ÉtapesEspaces → signe → chiffres
Formulenbr = nbr × 10 + chiffre
int	ft_atoi(char *str)
{
	int	i;
	int	sign;
	int	nbr;

	i = 0;
	sign = 1;
	nbr = 0;
	while (str[i] == ' ' || (str[i] >= 9 && str[i] <= 13))
		i++;
	if (str[i] == '-' || str[i] == '+')
	{
		if (str[i] == '-')
			sign = -1;
		i++;
	}
	while (str[i] >= '0' && str[i] <= '9')
	{
		nbr = nbr * 10 + str[i] - '0';
		i++;
	}
	return (nbr * sign);
}

ft_split

À CONNAÎTRE
ÉtapesCompter → malloc → trouver → copier
SortieTableau terminé par NULL
#include <stdlib.h>

static int	count_words(char *str, char sep)
{
	int	i;
	int	count;

	i = 0;
	count = 0;
	while (str[i])
	{
		while (str[i] == sep)
			i++;
		if (str[i])
			count++;
		while (str[i] && str[i] != sep)
			i++;
	}
	return (count);
}

static int	word_len(char *str, char sep)
{
	int	len;

	len = 0;
	while (str[len] && str[len] != sep)
		len++;
	return (len);
}

static char	*copy_word(char *str, int len)
{
	char	*word;
	int		i;

	word = malloc(sizeof(char) * (len + 1));
	if (!word)
		return (NULL);
	i = 0;
	while (i < len)
	{
		word[i] = str[i];
		i++;
	}
	word[i] = '\0';
	return (word);
}

char	**ft_split(char *str, char sep)
{
	char	**tab;
	int		i;
	int		j;
	int		len;

	tab = malloc(sizeof(char *) * (count_words(str, sep) + 1));
	if (!tab)
		return (NULL);
	i = 0;
	j = 0;
	while (str[i])
	{
		while (str[i] == sep)
			i++;
		if (!str[i])
			break ;
		len = word_len(&str[i], sep);
		tab[j] = copy_word(&str[i], len);
		if (!tab[j])
			return (NULL);
		j++;
		i += len;
	}
	tab[j] = NULL;
	return (tab);
}

Recherche / occurrences

PATTERN
int	find_char(char *str, char c)
{
	int	i;

	i = 0;
	while (str[i])
	{
		if (str[i] == c)
			return (i);
		i++;
	}
	return (-1);
}

int	count_char(char *str, char c)
{
	int	i;
	int	count;

	i = 0;
	count = 0;
	while (str[i])
	{
		if (str[i] == c)
			count++;
		i++;
	}
	return (count);
}

07 — Linked Lists

Les opérations classiques sur les listes.

ft_lstnew

LIST
t_list	*ft_lstnew(void *content)
{
	t_list	*node;

	node = malloc(sizeof(t_list));
	if (!node)
		return (NULL);
	node->content = content;
	node->next = NULL;
	return (node);
}

ft_lstadd_front

LIST
void	ft_lstadd_front(t_list **lst, t_list *new)
{
	if (!lst || !new)
		return ;
	new->next = *lst;
	*lst = new;
}

08 — Réflexes d'évaluation

Reconnaître rapidement le type d'exercice.

Patterns à reconnaître

RÉVISION
  • Parcourir une string → while (str[i])
  • Compter une string → ft_strlen
  • Copier → boucle + '\0'
  • Comparer → avancer tant que les caractères sont égaux
  • String → int → ft_atoi
  • String → tableau de mots → ft_split
  • Inverser → deux index + swap
  • Trier → deux boucles + comparaison
  • Allocation → malloc + vérification + free
  • Fichier → open → read → close
  • Afficher → write
Aucune fonction trouvée.