array_iteration.md
April 3, 2025 ยท View on GitHub
Iterating Over an Array
Iterating over an array is a fundamental operation in programming. Our library provides several ways to iterate over an array, but in this example, we'll focus on using a traditional for loop. Let's dive into how this works with a detailed example.
Example: Iterating Over an Array
Here's a step-by-step guide on how to iterate over an array:
-
Include the Necessary Header: Start by including the
CHashManipulatorOne.cheader file which contains the necessary definitions for our library.#include "CHashManipulatorOne.c" -
Initialize the Namespace and Modules: We need to initialize the
CHashNamespaceand its modules likeobject,array, andvalidator.CHashNamespace hash = newCHashNamespace(); CHashObjectModule obj = hash.object; CHashArrayModule array = hash.array; CHashValidatorModule validator = hash.validator; -
Create an Array: Use the
newCHashArrayfunction to create an array with initial elements.CHashObject *create() { return newCHashArray( hash.newString("aaa"), hash.newNumber(26), hash.newNumber(20), hash.newBool(true) ); } -
Get the Array Size: Use
hash.get_sizeto get the size of the array.int main() { CHashArray *element = create(); long size = hash.get_size(element); -
Iterate Over the Array: Use a
forloop to iterate over the array. Inside the loop, usearray.get_typeto determine the type of each element and then use the appropriate getter function to retrieve its value.for (int i = 0; i < size; i++) { int type = array.get_type(element, i); if (type == CHASH_STRING) { printf("%s\n", array.getString(element, i)); } if (type == CHASH_NUMBER) { printf("%lf\n", array.getNumber(element, i)); } if (type == CHASH_BOOL) { printf("%s\n", array.getBool(element, i) ? "true" : "false"); } } -
Check for Errors: Use
CHash_catchto handle any errors that might occur during the iteration.CHash_catch(element) { printf("%s", hash.get_error_message(element)); } -
Free the Memory: Don't forget to free the memory allocated for the array to prevent memory leaks.
hash.free(element); }
Full Code Example
Here's the complete code example that demonstrates how to iterate over an array:
#include "CHashManipulatorOne.c"
CHashNamespace hash;
CHashObjectModule obj;
CHashArrayModule array;
CHashValidatorModule validator;
CHashObject *create() {
return newCHashArray(
hash.newString("aaa"),
hash.newNumber(26),
hash.newNumber(20),
hash.newBool(true)
);
}
int main() {
hash = newCHashNamespace();
obj = hash.object;
array = hash.array;
validator = hash.validator;
CHashArray *element = create();
long size = hash.get_size(element);
for (int i = 0; i < size; i++) {
int type = array.get_type(element, i);
if (type == CHASH_STRING) {
printf("%s\n", array.getString(element, i));
}
if (type == CHASH_NUMBER) {
printf("%lf\n", array.getNumber(element, i));
}
if (type == CHASH_BOOL) {
printf("%s\n", array.getBool(element, i) ? "true" : "false");
}
}
CHash_catch(element) {
printf("%s", hash.get_error_message(element));
}
hash.free(element);
}
By following these steps, you can easily iterate over an array using our library. Remember to always check for errors and manage memory properly to ensure your program runs smoothly.