As the title says, I've become quite accustomed to handling char * types (character strings), so in training to handle pointer-type pointer-type variables I wrote an array process that can create any size. If there is something strange, please give it a try.
Define a pseudo array structure that can dynamically expand the array size in C language
/**
*Create a pseudo-subscript array
*/
typedef struct dummy_array{
char **value; // value =>value
int size; //Array size
char* (*getValue)(struct dummy_array*, int); //Function pointer to struct
} DummyArray;
Define an interface to handle the above structure
/**
*Get the value of any index for the array object passed as the first argument
*index shall satisfy the value of size of the structure member
* @param DummyArray* array
* @param int index
* @return char*
*/
char* getValue (DummyArray* array, int index) {
if (array == NULL) {
return NULL;
}
//Array index 0<= index < strlen(array)
if (index >= 0 && index < array->size) {
return (array->value[index]);
}
}
/**
*Generates an index array with the specified size
* @param int size
* @return DummyArray*
*/
DummyArray* makeArray (int size) {
DummyArray *array = NULL;
int make_size = 1;
//Allocate memory for one DummyArray object
array = malloc(make_size * sizeof(DummyArray));
if (array == NULL) {
return NULL;
}
//Char with the size of the int type variable passed as the first argument**Allocate type memory
array->value = (char **)malloc(size * sizeof(char *));
if (array->value == NULL) {
return NULL;
}
array->getValue = getValue;
array->size = size;
return array;
}
/**
*Extend the size of the value member of the DummyArray object passed as the first argument by one
* @param DummyArray* array
* @return DummyArray
*/
DummyArray* extendArray (DummyArray* array) {
char **temp;
temp = (char **)realloc(array->value, (array->size + 1) * sizeof(char *));
if (temp == NULL) {
return array;
}
if (temp != array->value) {
array->value = temp;
}
//Don't forget to add the size of the array
array->size++;
return array;
}
/**
*Push the target of the second argument to the first NULL value of the DummyArray object passed to the first argument
* @param DummyArray* array
* @param char* target
* @return int
*/
int appendValue (DummyArray* array, char* target) {
int index;
for (index = 0; index < array->size; index++) {
if (array->value[index] == NULL) {
array->value[index] = target;
return 1;
}
}
return 0;
}
For the above, prepare a member function so that you can handle the object of the structure without directly editing it.
The following is the main function that actually handles the object called DummyArray structure.
int main()
{
DummyArray* array;
char *input = NULL;
int array_index = 0;
printf("Please enter the size of the initial array>>>\r\n");
input = get_command_line();
//Cast the input string to int type
array_index = atoi(input);
if (array_index == 0) {
return 1;
}
//Generate DummyArray array
array = makeArray(array_index);
int i = 0;
printf("[0]From[%d]Please enter the values of the array up to>>>\r\n", array_index - 1);
for (i = 0; i < array->size; i++)
{
printf("[%d]Th>>>\r\n", i);
input = get_command_line();
if (appendValue(array, input) != 1 ) {
printf("Value Push failed");
exit(255);
}
}
//After entering all the values,Try expanding the size of the array
array = extendArray(array);
appendValue(array, "I expanded the array and added values.");
printf("Get the list of values held by the DummyArray object\r\n");
for (i = 0; i < array->size; i++)
{
printf("%s\r\n", array->getValue(array, i));
}
}
In the above
get_command_line()
There is a function called, but this is a self-made function that gets a character string of any size input from the command line.
If you build and run this
[root@1ef475d5fd40 tmp]#
[root@1ef475d5fd40 tmp]# ./a.out
Please enter the size of the initial array>>>
5 //Input from CLI
[0]From[4]Please enter the values of the array up to>>>
[0]Th>>>
AIUEO//Input from CLI
[1]Th>>>
Kakikukeko//Input from CLI
[2]Th>>>
SA Shi Su Se So//Input from CLI
[3]Th>>>
TA Chi Tsu Te to//Input from CLI
[4]Th>>>
What is it//Input from CLI
Get the list of values held by the DummyArray object
AIUEO
Kakikukeko
SA Shi Su Se So
TA Chi Tsu Te to
What is it
I expanded the array and added values.
[root@1ef475d5fd40 tmp]#
At the time of array initialization for the above Create a DummyArray object with members of a string array of any size Enter the sequential values from the CLI and In addition, extend the size of index with a self-made function called extendArray. Add a new const char * type value there
Recommended Posts