день добрый, товарищи колдуны! :о)
краткое строение проекта: - struct.x - описание структуры, массив/список данных/структур, функции работы со структурой/списком. - struct_cust.x - кастомные данные, создание экземпляров структуры, добавление в "список"
некоторые неудобства: - наполнение данных/добавление в "список" происходит в реализации/struct_cust.с - установка счетчика списка в хедере/struct_cust.h
по другому не получилось "реализовать", хотелось бы все логично/в одном месте: - создание данных/добавление в список - определение/установка счетчика
какие есть мнение/идеи/хаки-каки итд?!
- linux, gcc, c-pure
спасиб
main.c
| Код | #include <string.h> #include <stdlib.h> #include <stdio.h> #include <stdint.h> #include "main.h" #include "struct.h"
int main (void) { printf("### struct ###\n"); struct_list(); return 0; }
|
struct.h
| Код | struct data_struct { uint8_t num; void (*func)(uint8_t id); };
void struct_list(void);
|
struct.c
| Код | #include "struct.h" #include "struct_cust.h"
struct data_struct * lst[NUM];
void struct_list() { printf("struct_list()\n");
for(uint8_t i=0; i<NUM; i++) { printf("lst[%u].num = %u; ",i, lst[i]->num); if(lst[i]->func != NULL) { lst[i]->func(i); } printf("\n"); } }
|
struct_cust.h
| Код | #define NUM 3 void struct_cust_func(uint8_t id);
|
struct_cust.c
| Код | #include "struct.h" #include "struct_cust.h"
struct data_struct data1 = { .num = 1, .func = struct_cust_func }; struct data_struct data2 = { .num = 2, };// .func = struct_cust_func }; struct data_struct data3 = { .num = 3, .func = struct_cust_func };
extern struct data_struct * lst[NUM]; struct data_struct * lst[NUM] = { &data1, &data2, &data3 };
void struct_cust_func(uint8_t id) { printf("struct_cust_func(%u)", id); }
|
makefile
| Код | all: main.o struct.o struct_cust.o gcc -o main main.o struct.o struct_cust.o
main.o: main.c gcc -c main.c
struct.o: struct.c gcc -c struct.c
struct_cust.o: struct_cust.c gcc -c struct_cust.c
clean: rm -f *.o main
|
main out
| Код | ### struct ### struct_list() lst[0].num = 1; struct_cust_func(0) lst[1].num = 2; lst[2].num = 3; struct_cust_func(2)
|
*.txt -> *.txz |