-1

I've got this code, but it doesnt work, what's wrong? I try to make massive of struct with dynamic size(C language) after the second use of add_sala(); in main function Windows close programm. Please help to solve this problem! Thanks!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
char trash[50];
int dyn_sala_id=1;
typedef struct
{
    int id;
    char number[6];
    int persons;
    char tech_inf[256];
} sala;
sala *sala_;

int add_sala()
{
    int persons;
    char number[6], tech_inf[256];


    sala_ = (sala*)realloc(sala_,dyn_sala_id * sizeof(sala));

    printf("Wpisz numer sali(max. 5 znakow): ");
    fgets(number,6,stdin);
    if(strlen(number)>5)
    {
        printf("Numer musi byc nie wiecej, niz 5 znakow!\n");
        fflush(stdin);
        add_sala();
        return 0;
    }

    printf("Wpisz ilosc osob, ktora wmiesci sie w sale(max. 1000 osob): ");
    scanf("%d", &persons);
    if(persons==0 || persons>1000)
    {
        printf("Nie wolno wprowadzic litery oraz max. ilosc osob to 1000\n");
        fflush(stdin);
        add_sala();
        return 0;
    }

    printf("Wpisz info o wyposazeniu sali(max. 255 znakow): ");
    fgets(trash,50,stdin);
    fgets(tech_inf,256,stdin);
    if(strlen(tech_inf)>255)
    {
        printf("Info musi byc nie wiecej, niz 255 znakow!\n");
        fflush(stdin);
        add_sala();
        return 0;
    }

    sala_[dyn_sala_id].id = dyn_sala_id;
    strncpy(sala_[dyn_sala_id].number, number, 6);
    sala_[dyn_sala_id].persons = persons;
    strncpy(sala_[dyn_sala_id].tech_inf, tech_inf, 256);
    printf("\nSala zostala dodana!\n\n");
    printf("%d, %d, %s, %s",dyn_sala_id, persons, number, tech_inf);
    dyn_sala_id+=1;
    return 0;
}

int main()
{
    add_sala();
    printf("%s",sala_[1].number);
    add_sala();
    printf("'%s'",sala_[1].number);
    printf("'%s'",sala_[2].number);
    return 0;
}
Max Pan
  • 137
  • 1
  • 7
  • [Please see this discussion on why not to cast the return value of `malloc()` and family in `C`.](http://stackoverflow.com/q/605845/2173917). – Sourav Ghosh Dec 29 '16 at 17:27
  • Welcome to Stack Overflow. It would appear that you need to learn how to use a debugger to step line-by-line through your code, which will likely allow you to easily pinpoint the nature and location of the issue you're having. Using a debugger is, for all intents and purposes, required knowledge for any programmer. For more info, see [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Random Davis Dec 29 '16 at 17:49

1 Answers1

1

Arrays in C are indexed from 0, so in main() the array indexing is off by 1.

add_sala();
printf("%s",sala_[1].number);
add_sala();
printf("'%s'",sala_[1].number);
printf("'%s'",sala_[2].number);

Also in the function add_sala() it is clear that the first time it is called you have the global

int dyn_sala_id=1;

which you use to allocate memory for one record with

sala_ = (sala*)realloc(sala_,dyn_sala_id * sizeof(sala));

but a bit further down, the indexing is again off by 1, where there is plainly only one array element

sala_[dyn_sala_id].id = dyn_sala_id;

Then, in that same function (although I can't read the error messages) it seems strange that after an apparent bad input, you recurse the function. Also, you have undefined behaviour with

fflush(stdin);

and I have not looked further, because the code will not work.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56