Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Here are the main functions that I'm using for my projects. A lot of them copy the behavior of the most famous functions from the string.h library or other ones. It was a good project to understand how they are working and how to create your own library.

Notifications You must be signed in to change notification settings

SimonIsCoding/libft

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

19 Commits

Repository files navigation

Libft - C Library Implementation

πŸ“š Overview

This project is a custom implementation of the C standard library functions, commonly known as "libft" in programming education. It includes both basic string and memory manipulation functions, as well as bonus linked list operations.

Final score

🎯 What is Libft?

Libft is a foundational project that teaches:

  • Memory management in C
  • String manipulation without using standard library functions
  • Linked list data structures
  • Pointer arithmetic
  • C programming best practices

🧠 Essential C Concepts for Beginners

1. Pointers - The Foundation

A pointer is a variable that stores the memory address of another variable. Think of it as a "house address" that tells you where to find the actual data.

int number = 42; // A regular variable
int *pointer = &number; // A pointer that stores the address of 'number'

Visual Representation:

Memory Layout:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ number β”‚ β”‚ pointer β”‚
β”‚ 42 β”‚ β”‚ 0x7fff... β”‚
β”‚ 0x7fff... │◄───│ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Why Pointers Matter:

  • Dynamic memory allocation: Create variables at runtime
  • Pass by reference: Modify original variables in functions
  • Data structures: Build complex structures like linked lists
  • Efficiency: Avoid copying large data

🧸 Pointers Explained Easily!

Imagine you have a treasure map! πŸ—ΊοΈ

  • The treasure map is like a pointer - it doesn't have the treasure itself, but it tells you where to find it
  • The treasure is like your data (a number, a word, etc.)
  • The X marks the spot is like the memory address

🎨 Super Simple Visual:

🏠 Your House (where you keep your toy box)
 β”‚
 β–Ό (your toy box is here!)
πŸ“¦ Toy Box = Your Data (like the number 42)
 β”‚
 β–Ό (this is your "map" to find it!)
πŸ—ΊοΈ Treasure Map = Pointer (says "go to your house, find the toy box!")
When you follow the map β†’ you find your toy! 🎁

🎯 Real Example:

int my_toy = 42; // πŸ“¦ This is your toy (data)
int *my_map = &my_toy; // πŸ—ΊοΈ This is your map (pointer)
// &my_toy means "where is my_toy located?"

πŸŽͺ Memory Adventure:

Memory City:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ 🏠 House 1: my_toy = 42 (your toy is here!) β”‚
β”‚ 🏠 House 2: my_map = "go to House 1!" (your map) β”‚
β”‚ 🏠 House 3: empty β”‚
β”‚ 🏠 House 4: empty β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Your map (my_map) points to House 1 where your toy (42) lives! πŸ βž‘οΈπŸ“¦

Remember: A pointer is just a friendly helper that remembers where your stuff is! πŸ€—

2. Linked Lists - Dynamic Data Structures

A linked list is a data structure where each element (node) contains data and a pointer to the next element.

Structure Definition:

typedef struct s_list
{
 void *content; // The data stored in this node
 struct s_list *next; // Pointer to the next node
} t_list;

Visual Representation:

Linked List Structure:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Node 1 β”Œβ”€β”€β”€β–Ά β”‚ Node 2 β”Œβ”€β”€β”€β–Ά β”‚ Node 3 β”Œβ”€β”€β”€β–Ά β”‚ NULL β”‚
β”‚ content: "A"β”‚ β”‚ content: "B"β”‚ β”‚ content: "C"β”‚ β”‚ β”‚
β”‚ next: β”€β”€β”€β”€β”€β”€β”˜ β”‚ next: β”€β”€β”€β”€β”€β”€β”˜ β”‚ next: β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸŽͺ Linked Lists are like a Train! πŸš‚

Think of a linked list like a train with wagons:

πŸš‚ Train Adventure:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ πŸš‚ Engine β”Œβ”€β”€β”€β–Ά β”‚ πŸšƒ Wagon 1 β”Œβ”€β”€β”€β–Ά | πŸšƒ Wagon 2 β”Œβ”€β”€β”€β–Ά β”‚ πŸšƒ Wagon 3 β”‚
β”‚ Driver: A β”‚ β”‚ Cargo: B β”‚ β”‚ Cargo: C β”‚ β”‚ Cargo: D β”‚
β”‚ Next: β”€β”€β”€β”€β”€β”˜ β”‚ Next: β”€β”€β”€β”€β”€β”˜ β”‚ Next: β”€β”€β”€β”€β”€β”˜ β”‚ Next: πŸ›‘ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

How it works:

  • Each wagon knows what's inside (cargo) 🎁
  • Each wagon knows which wagon comes next (next) ➑️
  • The last wagon points to nothing (πŸ›‘ = NULL)
  • You can add new wagons anywhere! πŸ†•
  • You can remove wagons without breaking the train! βœ‚οΈ

🎯 Real Train Example:

// πŸš‚ Create a train with one wagon
t_list *train = ft_lstnew("Apple"); // Engine with an apple
// πŸšƒ Add more wagons
ft_lstadd_back(&train, ft_lstnew("Banana")); // Add banana wagon
ft_lstadd_back(&train, ft_lstnew("Cherry")); // Add cherry wagon
// πŸŽͺ Now your train is: Apple β†’ Banana β†’ Cherry β†’ πŸ›‘

3. Memory Management

Stack vs Heap:

Memory Layout:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Stack β”‚
β”‚ (Automatic variables, function β”‚
β”‚ parameters, return addresses) β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Heap β”‚
β”‚ (Dynamically allocated memory β”‚
β”‚ with malloc, calloc, realloc) β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

🍽️ Memory Management Like Different Types of Restaurants! πŸ•

Think of your computer's memory like different types of restaurants:

🍽️ Computer Memory linked with Food:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ 🍽️ Fast Food (Stack): β”‚
β”‚ - Like McDonald's - quick service! β”‚
β”‚ - You order, eat, and leave β”‚
β”‚ - Staff cleans up automatically after you β”‚
β”‚ - No need to worry about dishes β”‚
β”‚ - Perfect for quick meals (small data) β”‚
β”‚ β”‚
β”‚ 🍽️ Your Kitchen (Heap): β”‚
β”‚ - You prepare what you need (malloc) β”‚
β”‚ - You decide how much to eat β”‚
β”‚ - YOU MUST clean your own plate (free)! β”‚
β”‚ - If you forget to clean β†’ plates pile up! πŸ—‘οΈ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

🎯 Memory Adventure Example:

// 🍽️ Fast Food (Stack - automatic cleanup)
int my_number = 42; // Order, eat, leave - staff cleans up!
// 🍽️ Self-Service Buffet (Heap - manual cleanup)
int *big_array = malloc(100 * sizeof(int)); // Take a big plate
// ... use the plate ...
free(big_array); // Clean your own plate!

Important:

  • 🍽️ Stack = Fast food (automatic cleanup)
  • 🍽️ Heap = your kitchen (YOU must clean up!)
  • 🧹 Always clean your buffet plate (free) or dishes pile up! πŸ—‘οΈ
  • πŸ“ Both restaurants exist at the same time - no hierarchy!

πŸ“ Project Structure

Core Functions (Part 1)

String Functions:
β”œβ”€β”€ ft_strlen.c - Calculate string length
β”œβ”€β”€ ft_strchr.c - Find character in string
β”œβ”€β”€ ft_strrchr.c - Find last occurrence of character
β”œβ”€β”€ ft_strncmp.c - Compare strings
β”œβ”€β”€ ft_strnstr.c - Find substring
β”œβ”€β”€ ft_strdup.c - Duplicate string
β”œβ”€β”€ ft_substr.c - Extract substring
β”œβ”€β”€ ft_strjoin.c - Concatenate strings
β”œβ”€β”€ ft_strtrim.c - Remove characters from ends
β”œβ”€β”€ ft_split.c - Split string by delimiter
β”œβ”€β”€ ft_strmapi.c - Apply function to each character
β”œβ”€β”€ ft_striteri.c - Apply function with index
β”œβ”€β”€ ft_strlcpy.c - Copy string with size limit
└── ft_strlcat.c - Concatenate with size limit
Memory Functions:
β”œβ”€β”€ ft_memset.c - Set memory to value
β”œβ”€β”€ ft_bzero.c - Set memory to zero
β”œβ”€β”€ ft_memcpy.c - Copy memory
β”œβ”€β”€ ft_memmove.c - Move memory (handles overlap)
β”œβ”€β”€ ft_memchr.c - Find byte in memory
└── ft_memcmp.c - Compare memory
Character Functions:
β”œβ”€β”€ ft_isalpha.c - Check if alphabetic
β”œβ”€β”€ ft_isdigit.c - Check if digit
β”œβ”€β”€ ft_isalnum.c - Check if alphanumeric
β”œβ”€β”€ ft_isascii.c - Check if ASCII
β”œβ”€β”€ ft_isprint.c - Check if printable
β”œβ”€β”€ ft_toupper.c - Convert to uppercase
└── ft_tolower.c - Convert to lowercase
Conversion Functions:
β”œβ”€β”€ ft_atoi.c - String to integer
β”œβ”€β”€ ft_itoa.c - Integer to string
└── ft_calloc.c - Allocate and zero memory
Output Functions:
β”œβ”€β”€ ft_putchar_fd.c - Write character to file descriptor
β”œβ”€β”€ ft_putstr_fd.c - Write string to file descriptor
β”œβ”€β”€ ft_putendl_fd.c - Write string with newline
└── ft_putnbr_fd.c - Write number to file descriptor

Bonus Functions (Part 2) - Linked Lists

Linked List Functions:
β”œβ”€β”€ ft_lstnew.c - Create new list node
β”œβ”€β”€ ft_lstadd_front.c - Add node to beginning
β”œβ”€β”€ ft_lstadd_back.c - Add node to end
β”œβ”€β”€ ft_lstsize.c - Count nodes in list
β”œβ”€β”€ ft_lstlast.c - Find last node
β”œβ”€β”€ ft_lstdelone.c - Delete single node
β”œβ”€β”€ ft_lstclear.c - Delete entire list
β”œβ”€β”€ ft_lstiter.c - Apply function to each node
└── ft_lstmap.c - Create new list with transformed data

πŸš€ How to Use

Compilation

# Compile the library
make
# Compile with bonus functions
make bonus
# Clean object files
make clean
# Clean everything
make fclean
# Recompile everything
make re

Usage Example

#include "libft.h"
int main(void)
{
 // String operations
 char *str = "Hello, World!";
 printf("Length: %zu\n", ft_strlen(str));
 
 // Create a linked list
 t_list *list = ft_lstnew("First node");
 ft_lstadd_back(&list, ft_lstnew("Second node"));
 
 // Print list size
 printf("List size: %d\n", ft_lstsize(list));
 
 // Clean up
 ft_lstclear(&list, free);
 return (0);
}

πŸ” Key Learning Concepts

1. Pointer Arithmetic

char *str = "Hello";
// str[0] is equivalent to *(str + 0)
// str[1] is equivalent to *(str + 1)

2. Memory Safety

  • Always check if malloc returns NULL
  • Free allocated memory to prevent memory leaks
  • Be careful with buffer overflows

3. String Handling

  • C strings are null-terminated (0円)
  • String length doesn't include the null terminator
  • Always allocate space for the null terminator

4. Linked List Operations

// Creating a new node
t_list *new_node = ft_lstnew("data");
// Adding to front (becomes new head)
ft_lstadd_front(&head, new_node);
// Adding to back (becomes new tail)
ft_lstadd_back(&head, new_node);
// Traversing the list
t_list *current = head;
while (current != NULL)
{
 // Process current->content
 current = current->next;
}

πŸŽ“ Common Pitfalls for Beginners

  1. Forgetting to check malloc return: Always check if malloc returns NULL
  2. Buffer overflows: Don't write beyond allocated memory
  3. Memory leaks: Always free what you allocate
  4. Null pointer dereference: Check pointers before using them
  5. String length confusion: Remember null terminator
  6. Linked list cycles: Ensure the last node points to NULL

πŸ”§ Testing

You can test individual functions by uncommenting the test code at the bottom of each .c file and compiling with:

cc -Wall -Wextra -Werror ft_strlen.c -o test_strlen
./test_strlen

🀝 Contributing

This is an educational project. Feel free to:

  • Report bugs
  • Suggest improvements
  • Add more examples
  • Improve documentation

Happy coding! πŸš€

About

Here are the main functions that I'm using for my projects. A lot of them copy the behavior of the most famous functions from the string.h library or other ones. It was a good project to understand how they are working and how to create your own library.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /