In my program there is an input of elements of structural type and input of 2 numbers. After actually entering the menu at the top, I’m showing the status, whether the data is entered (YES, NO) checking if there is no NULL in the pointer (if there is no NULL, then the memory is allocated, and if the memory is allocated, then the data was recorded.) After writing the data, the status "NO" does not go to "YES", I can not understand what's the matter, maybe we have made some pointers ...
#include "stdafx.h" #include "stdio.h" #include "locale.h" #include "stdlib.h" //Глобально задаем структуру struct base { int first; int second; int third; } *list = NULL; //Функции //Проверка выделения памяти void CheckMem(void *list); //Ввод диапазона void Range(int *n, int *N); //Меню ввода данных через консоль void ConsoleRecord(struct base *list, int *len,int *n,int *N); //Запись элементов стр. типа через консоль void ConsoleScan(struct base *list, int *len); //Чистим мусор после ввода void CleanStdin(); int main(void) { setlocale(LC_ALL, ""); //Диапазон int *n; n = NULL; int *N; N = NULL; //Длина массива стр. элементов int *len; len = NULL; char number; printf("Добро пожаловать."); do { printf("Выберите необходимый пункт:\n"); printf("1) Запись данных с помощью консоли\n"); printf("2) Запись данных с помощью текстового файла\n"); printf("3) Выход\n"); number = getchar(); CleanStdin(); switch(number){ case '1': system("cls"); ConsoleRecord(list, len, n, N); break; case '2': system("cls"); printf("Еще не готово :(\n"); break; case '3': break; default: system("cls"); printf("Вы ничего не выбрали.\n"); break; } } while(number != '3'); //Освобождение памяти free(list); free(len); free(n); free(N); return 0; } void CheckMem(void *p) { if (p == NULL) printf("Память не выделена. Аварийное завершение программы.\n"), system("pause"), exit(1); } void Range(int *n, int *N) { if (n == NULL) { n = (int*)malloc(sizeof(int)), CheckMem(n); N = (int*)malloc(sizeof(int)), CheckMem(N); } printf("Введите диапазон ОТ:\n"); scanf_s("%d", n); CleanStdin(); printf("Введите диапазон ДО:\n"); scanf_s("%d", N); CleanStdin(); printf("Ваш диапазон: %d-%d\n", *n, *N); system("pause"); CleanStdin(); system("cls"); } void ConsoleRecord(struct base *list,int *len, int *n,int *N) { char number; printf("Вы выбрали запись данных с помощью консоли."); printf("\n------------------------------------------\n"); do { printf("Диапазон: "); if (n == NULL) { printf("НЕТ"); } else { printf("ДА"); } printf(". Элементы структурного типа: "); if (len == NULL) { printf("НЕТ"); } else { printf("ДА"); } printf(".\n------------------------------------------\n"); printf("Выберите необходимый пункт:\n"); printf("1) Ввести элементы структурного типа\n"); printf("2) Ввести диапазон\n"); printf("3) Обработка структуры\n"); printf("4) Выход в основное меню\n"); number = getchar(); CleanStdin(); switch (number) { case '1': system("cls"); ConsoleScan(list,len); break; case '2': system("cls"); Range(n,N); break; case '3': system("cls"); printf("Еще не готово.\n"); break; case '4': system("cls"); break; default: system("cls"); printf("Вы ничего не выбрали.\n"); break; } } while (number != '4'); } void CleanStdin() { int c; do { c = getchar(); } while (c != '\n' && c != EOF); } void ConsoleScan(struct base *list, int *len) { if (len == NULL) { len = (int*)malloc(sizeof(int)), CheckMem(len); } else { printf("Идет перезапись элементов массива структурного типа.\n"); free(list); return; } printf("Введите кол-во элементов структурного типа:\n"); scanf_s("%d", len); CleanStdin(); list = (struct base*)malloc((*len) * sizeof(struct base)), CheckMem(list); int i; for (i = 0; i < *len; i++) { printf("Введите поля %d-ого элемента массива структурного типа: ", i + 1); scanf_s("%d", &(list->first)); scanf_s("%d", &(list->second)); scanf_s("%d", &(list->third)); } CleanStdin(); system("pause"); system("cls"); }