Skip to content
AlisonCameloPublic

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This project has been created as part of the 42 curriculum by acamelo.

C Piscine C 01 - Pointers, Memory Manipulation & Arithmetic

Description

C 01 shifts the focus from basic control flow to the core mechanic of the C programming language: pointers and memory address manipulation. The primary objective of this module is to demystify how variables are stored in stack memory, how to pass variables by reference to allow persistent mutation across functions, and how arrays behave as contiguous blocks of memory.

Through an intensive series of exercises, this project explores pointer dereferencing, multi-layered pointers (pointers to pointers up to $9$ levels deep), implementation of basic arithmetic algorithms (division, modulo, value swapping), and array sorting routines from scratch—all while maintaining the rigorous formatting and performance constraints enforced by the 42 Norm.


Instructions

Prerequisites

To compile and execute these programs, you require a standard C compiler (gcc or clang) installed within a UNIX-like environment.

Compiling and Testing

As with previous piscine modules, these early exercises are compiled individually. Each directory contains an independent file containing the core functional logic alongside an operational main.c framework for instant verification.

  • To compile a specific exercise using the standard 42 security flags:
cc -Wall -Wextra -Werror ft_ultimate_ft.c -o test_program
  • To run the generated binary executable:
./test_program

Detailed Description of Exercises

Every exercise targets a specific application of pointers, addressing, or array indexing:

1. Pointer Dereferencing & Indirection Levels

  • **ex00: ft_ft.c** – The fundamental introduction to pointers. Takes a pointer to an integer as a parameter and modifies the value it points to, updating it directly to 42.
  • **ex01: ft_ultimate_ft.c** – Introduces extreme levels of memory indirection by taking a pointer to a pointer (up to 9 levels: int *********) and dereferencing through all layers to modify the underlying integer value.
  • **ex02: ft_swap.c** – Leverages pointer-based passing by reference to swap the values of two integers stored at different memory addresses using a temporary holding variable.

2. Basic Arithmetic Operations by Reference

  • **ex03: ft_div_mod.c** – Divides two integers and stores the resulting quotient and remainder directly into two separate memory addresses passed as pointer parameters.
  • **ex04: ft_ultimate_div_mod.c** – Performs the same division and modulo operation, but optimizes memory usage by overwriting the initial values stored inside the parameters themselves.

3. Strings & Memory Contiguity

  • **ex05: ft_putstr.c** – Displays a string of characters to the standard output by iterating through an array of chars via character pointers until it hits the null-terminator (\0).
  • **ex06: ft_strlen.c** – Replicates the standard strlen functionality by counting and returning the total number of characters in a string by tracking pointer offsets until the null-terminator is reached.

4. Array Manipulation & Sorting

  • **ex07: ft_rev_int_tab.c** – Reverses the order of elements within an integer array in-place. It utilizes two distinct pointer boundaries (start and end indexes) that move inward toward the center of the memory block.

Technical Decisions & Challenges

The Mechanics of 9-Level Indirection (int *********)

Developing ft_ultimate_ft requires tracing how the stack handles nested memory addresses. A common mistake when managing multiple pointer levels is losing track of the asterisk syntax or mismatching assignments. The function safely reaches the baseline data segment by applying nine consecutive dereferences (*********p = 42;), which forces the hardware to traverse the nested memory references step-by-step until it updates the target integer.

In-Place Array Mutation Without Auxiliary Arrays

When reversing an integer array in ft_rev_int_tab, memory constraints prohibit allocating a secondary array to hold values. To solve this efficiently, an in-place dual-pointer swapping strategy was applied. By initializing a pointer at index 0 and another at size - 1, elements are swapped using a single temporary variable while moving both index pointers inward. The loop terminates seamlessly when the pointers meet or cross at the center ($size / 2$).


Resources

References & Learning Materials

  • C Memory Model Layouts: Studied stack frame allocation behavior to fully grasp how local variables lose scope while variables passed via pointers retain mutations across function boundaries.
  • Pointer Arithmetic Manuals: Reviewed how array tracking (tab[i]) functions under the hood as direct pointer notation updates (*(tab + i)).
  • Cluster Peer Feedback: The 42 Madrid cluster was an excellent testing ground to swap boundary test scripts, verifying that edge cases—such as passing empty strings to ft_strlen or zero-sized arrays to ft_rev_int_tab—did not cause segmentation faults.

AI Usage Statement

Artificial Intelligence was integrated into this project as a structural and educational advisor under the following rules:

  • Memory Trace Analysis: Used to mentally map out complex multi-pointer references and verify code flow safety under strict compiler warnings.
  • Technical Editing: AI assisted in organizing, structuring, and polishing this technical README.md to ensure a cohesive presentation for recruiters ahead of the Web Summit.
  • Note: Every single line of code was written manually, line-by-line, adhering strictly to the 42 Norm guidelines.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages