ft_stralen - 1Fr3aK2/Cub3d GitHub Wiki

📝 ft_stralen

This function calculates the length of a 2D array of strings (array of char pointers) by counting the number of non-null elements.

⚙️ Parameters

Parameter Type Description
arr char** A pointer to the array of strings (array of char pointers).

🔁 Returns

Return value Description
size_t The number of strings in the array (not counting the null pointer at the end).

📖 Description

The ft_stralen function takes a 2D array of strings as input and counts how many non-null pointers exist in the array. It loops through the array until it encounters a null pointer (NULL), which indicates the end of the array, and then returns the count of strings.

  • If the input array is NULL, the function returns 0.
  • The function iterates over each element in the array, incrementing the counter until it reaches the end.

💡 Example Usage

char *arr[] = {"Hello", "world", "42", NULL}; // Example array of strings
int len = ft_stralen(arr); // Returns 3, as there are 3 non-null strings

printf("Array length: %d\n", len);