краткое строение проекта:
Код: Выделить всё
- 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 data_struct
{
uint8_t num;
void (*func)(uint8_t id);
};
void struct_list(void);
Код: Выделить всё
#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");
}
}
Код: Выделить всё
#define NUM 3
void struct_cust_func(uint8_t id);
Код: Выделить всё
#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);
}
Код: Выделить всё
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
Код: Выделить всё
### struct ###
struct_list()
lst[0].num = 1; struct_cust_func(0)
lst[1].num = 2;
lst[2].num = 3; struct_cust_func(2)