Background
- Writing a library with two kind of functions
- process array
- process array char
- This data structure is accessed by the project inner loop
Situation
I attempt to encapsulate the array declaration with its initialization as a step to keep related code together.
I am aware I will need to pass an array pointer to each function and wonder about performance.
Version 1 :
#include <stdio.h>
typedef struct char_array {
unsigned char uchar;
char achar;
} char_array;
#define CA_SIZE 1000000
char_array *ptr_array_one;
char_array *ptr_array_two;
main() {
char_array_init(ptr_array_one);
char_array_search(ptr_array_one);
char_array_init(ptr_array_two);
// etc.
}
char_array_init(struct char_array *ptr_array) {
static char_array ptr_array[CA_SIZE];
ptr_array[0].uchar= 'a';
ptr_array[5].uchar= 'b';
printf("%c\n\n", ptr_array[0].uchar);
}
char_array_search(struct char_array *ptr_array){
printf("%c\n\n", ptr_array[5].uchar);
}
I also tried Version 2 :
#include <stdio.h>
typedef union char_array {
unsigned char uchar;
char achar;
} char_array;
#define CA_SIZE 1000000
char_array *ptr_array_one;
main() {
ptr_array_one = char_array_init(ptr_array_one);
}
union * char_array_init(union char_array ptr_array) {
static char_array char_array[CA_SIZE];
char_array[0].uchar= 'a';
char_array[5].uchar= 'b';
printf("%c\n\n", char_array[0].uchar);
return &char_array;
}
I cannot get this to compile either.
Question
I am looking for a solution to achieve this coding attempt, inspired by this code
I am trying to dynamically declare two or more static arrays, for a pointer (ptr_array_one, ptr_array_two, ...) to reference them.
char_array_init()
- to declare and initialize a different array each time it is called
- to place array address into ptr_array_one, ptr_array_two, ...
char_array_search() intends to access a specific array, hence receives a char_array structure pointer as argument.
This code is my attempt to isolate the sought feature rather than the actual inner loop.
This code does not compile successfully. It's the best I can come up with.