works:programmer:cpp:struct-pointer-array
Массив из Struct указателей
#include <iostream> #include <cstring> using namespace std; struct Task { void* next; bool active; char* title; }; Task* first; Task* addTask(const char* title) { Task* t = first; while (t != NULL && t->active != false && t->next != NULL) t = (Task*) t->next; if (t->active == true) { t->next = (void*) malloc(sizeof(Task)); t = (Task*) t->next; } free(t->title); t->active = true; t->title = (char*) malloc(strlen(title)+sizeof(char)); memcpy(t->title, title, strlen(title)); return t; } void destroy_tasks(Task* first) { Task* next = (Task*) first->next; free(first->title); free(first); first = NULL; if (next != NULL) { destroy_tasks(next); } } int main() { first = (Task*) malloc(sizeof(Task)); first->next = NULL; addTask("Hello world"); addTask("Hello univerce"); addTask("Hello Enigma"); Task* t = first; while (t != NULL) { if (t->active) { cout << t->title << endl; } t = (Task*) t->next; } destroy_tasks(first); cout << "end" << endl; return 0; }
works/programmer/cpp/struct-pointer-array.txt · Последнее изменение: 2019/08/06 21:05 — 127.0.0.1