Problem when trying to copy a struct in C - c

So I am starting to use C, and I have some problems almost always with memory allocation. Basically I am getting stuck when I am trying to copy a struct to another struct.
I will put you the code here:
The headers file is:
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
//declaration constants
#define MAX_NAME 15+1
#define SEATS_PERCENTAGE 0.95
#define IN_TIME 60
#define OUT_TIME 120
//type declarations
typedef enum {FORBIDDEN, ALLOWED_WITH_COMPANION, ALLOWED} tFairgroundRideAccess;
typedef struct{
tFairgroundRideAccess lessThan100;
tFairgroundRideAccess between100_120;
tFairgroundRideAccess between120_140;
tFairgroundRideAccess greaterThan140;
} tFairgroundRideHeightRequirement;
typedef struct {
char name[MAX_NAME];
tFairgroundRideHeightRequirement accessHeight;
int durationTrip;
int numPersonsTrip;
int peopleInQueue;
} tFairgroundRide;
tFairgroundRide myFairgroundRide;
//define function headers
void readFairgroundRide(tFairgroundRide *fRide);
void writeFairgroundRide(tFairgroundRide fRide);
void copyFairgroundRide(tFairgroundRide fRideSrc, tFairgroundRide *fRideDst);
int waitingTime (tFairgroundRide fRide, int people);
bool accessWithoutCompanion (tFairgroundRide fRide, int height);
void selectFairgroundRide (tFairgroundRide fRide1, tFairgroundRide fRide2, int people1, int people2, int height2);
The file with the functions is the following one (what I want to check why I am not allocating properly in memory the new copy in the function copyFairgroundRide):
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include "fairgroundRide.h"
void readFairgroundRide(tFairgroundRide *fRide){
printf("NAME >> \n");
scanf("%s", fRide->name);
getchar();
printf("ACCESS HEIGHT, LESSTHAN100 (0-FORBIDDEN, 1-ALLOWED_WITH_COMPANION, 2-ALLOWED) >> \n");
scanf("%d", &fRide->accessHeight.lessThan100);
getchar();
printf("ACCESS HEIGHT, BETWEEN100_120 (0-FORBIDDEN, 1-ALLOWED_WITH_COMPANION, 2-ALLOWED) >> \n");
scanf("%d", &fRide->accessHeight.between100_120);
getchar();
printf("ACCESS HEIGHT, BETWEEN120_140 (0-FORBIDDEN, 1-ALLOWED_WITH_COMPANION, 2-ALLOWED) >> \n");
scanf("%d", &fRide->accessHeight.between120_140);
getchar();
printf("ACCESS HEIGHT, GREATERTHAN140 (0-FORBIDDEN, 1-ALLOWED_WITH_COMPANION, 2-ALLOWED) >> \n");
scanf("%d", &fRide->accessHeight.greaterThan140);
getchar();
printf("TRIP DURATION >> \n");
scanf("%d", &fRide->durationTrip);
getchar();
printf("NUMBER OF PERSONS ON A TRIP >> \n");
scanf("%d", &fRide->numPersonsTrip);
getchar();
}
void writeFairgroundRide(tFairgroundRide fRide){
printf("NAME: %s\n", fRide.name);
printf("ACCESS HEIGHT, LESSTHAN100 (0-FORBIDDEN, 1-ALLOWED_WITH_COMPANION, 2-ALLOWED): %d\n",fRide.accessHeight.lessThan100);
printf("ACCESS HEIGHT, LESSTHAN100 (0-FORBIDDEN, 1-ALLOWED_WITH_COMPANION, 2-ALLOWED): %d\n",fRide.accessHeight.between100_120);
printf("ACCESS HEIGHT, LESSTHAN100 (0-FORBIDDEN, 1-ALLOWED_WITH_COMPANION, 2-ALLOWED): %d\n",fRide.accessHeight.between120_140);
printf("ACCESS HEIGHT, LESSTHAN100 (0-FORBIDDEN, 1-ALLOWED_WITH_COMPANION, 2-ALLOWED): %d\n",fRide.accessHeight.greaterThan140);
printf("TRIP DURATION: %d\n", fRide.durationTrip);
printf("NUMBER OF PERSONS ON A TRIP: %d\n", fRide.numPersonsTrip);
}
void copyFairgroundRide(tFairgroundRide fRideSrc, tFairgroundRide *fRideDst){
strcpy(fRideDst->name,fRideSrc.name);
fRideDst->accessHeight.lessThan100 = fRideSrc.accessHeight.lessThan100;
fRideDst->accessHeight.between100_120 = fRideSrc.accessHeight.between100_120;
fRideDst->accessHeight.between120_140 = fRideSrc.accessHeight.between120_140;
fRideDst->accessHeight.greaterThan140 = fRideSrc.accessHeight.greaterThan140;
fRideDst->durationTrip = fRideSrc.durationTrip;
fRideDst->numPersonsTrip = fRideSrc.numPersonsTrip;
fRideDst->peopleInQueue = fRideSrc.peopleInQueue;
}
int waitingTime (tFairgroundRide fRide, int people){
int result;
result = ((IN_TIME+OUT_TIME+(fRide.durationTrip*60)) * (fRide.numPersonsTrip * SEATS_PERCENTAGE) * people);
return result;
}
bool accessWithoutCompanion (tFairgroundRide fRide, int height){
if (height < 100 && fRide.accessHeight.lessThan100 == 2){
return true;
}
if(height >= 100 && height<120 && fRide.accessHeight.between100_120 == 2){
return true;
}
if(height >= 120 && height<=140 && fRide.accessHeight.between120_140 == 2){
return true;
}
if (height > 140 && fRide.accessHeight.greaterThan140 == 2){
return true;
}else{
return false;
}
}
void selectFairgroundRide (tFairgroundRide fRide1, tFairgroundRide fRide2, int people1, int people2, int height2){
if((accessWithoutCompanion(fRide1,height2) == true) && (waitingTime(fRide1,people1)<=waitingTime(fRide2,people2))){
copyFairgroundRide(fRide1,&myFairgroundRide);
}
if((accessWithoutCompanion(fRide1,height2) == true) && (accessWithoutCompanion(fRide2,height2)==false)){
copyFairgroundRide(fRide1,&myFairgroundRide);
}
if((accessWithoutCompanion(fRide1,height2) == false) && (accessWithoutCompanion(fRide2,height2))){
copyFairgroundRide(fRide2,&myFairgroundRide);
}
}
And the main code is:
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include "fairgroundRide.h"
int main(){
tFairgroundRide myFairgroundRide;
tFairgroundRide fairgroundRide1;
tFairgroundRide fairgroundRide2;
int height1,people1,people2;
printf("ENTER DATA FOR FIRST FAIRGROUND RIDE >>\n");
readFairgroundRide(&fairgroundRide1);
printf("ENTER THE PEOPLE IN THE QUEUE OF FAIRGROUND RIDE 1 >> \n");
scanf("%d", &people1);
getchar();
printf("ENTER DATA FOR SECOND FAIRGROUND RIDE >>\n");
readFairgroundRide(&fairgroundRide2);
printf("ENTER THE PEOPLE IN THE QUEUE OF FAIRGROUND RIDE 2 >> \n");
scanf("%d", &people2);
getchar();
printf("ENTER THE HEIGHT >> \n");
scanf("%d", &height1);
getchar();
selectFairgroundRide(fairgroundRide1,fairgroundRide2,people1,people2,height1);
printf("RESULTS:\n");
writeFairgroundRide(myFairgroundRide);
getchar();
return 0;
}
So when I am running the program I can get all the info of both inputs, faigroundRide1 and fairgroundRide2, but if I copy it to another struct called myFairgroundRide I start getting weird characters and numbers, and I know that is due to memory allocation but I cannot find why or where is the issue. If you need further explanations about the code or what is my doubt just let me know and I will try to re-do it in another way.
Thanks in advance,
Jorge.

You have two myFairgroundRide. One in global scope, and one as local variable in main. In selectFairgroundRide you copy to the global one, but later in main you print the local one.
And by the way, copyFairgroundRide can be simplified:
void copyFairgroundRide(tFairgroundRide fRideSrc, tFairgroundRide *fRideDst)
{
*fRideDst = fRideSrc;
}

Related

when i compile it give me this error can anyone help me?

this is code written in c
#include <stdio.h>
#include "simpio.h"
#include "genlib.h"
#include <stdbool.h>
bool Valid_Time(int h,int min,int sec);
int main()
{
int h,min,sec;
printf("Dwse tis ores: ");
h=GetInteger();
printf("Dwse ta lepta: ");
min=GetInteger();
printf("Dwse ta defterolepta: ");
sec=GetInteger();
if ( Valid_Time (int h,int min,int sec) == true)
{
printf("Valid: yes");
}
else
{
printf("Valid: no");
}
return 0;
}
bool Valid_Time(int h,int min,int sec)
{
bool valid;
valid=true;
if(h<0 || h>23)
{
valid=false;
}
if(min<0 || min>59)
{
valid=false;
}
if(sec<0 || sec>59)
{
valid=false;
}
return valid;
}
error:expected expession befor 'int'
error:too few arguments to function 'Valid_Time'
i cant undersand why there is an error
why does this error pop up
The function call has type parameters in your usage. Remove the type specification when using the function.
Try this
Valid_Time (h,min,sec) == true

Structur Pointer C

I know I for sure misunderstand how to use pointers again. So here is my code. Would be nice if you all can help me. The program is simple. You write values in a structure array and print them out. Even so it would be nice if someone could explain to me when to use double pointers and how to use them probably.
#include <stdio.h>
#include <stdlib.h>
#define MAXA 3
typedef enum{
FOOD,
ART,
OTHERS
}TKindOfArticle;
typedef struct{
int number;
char description[31+1];
int sellingGrossPrice;
int vat;
int minimumStockLevel;
TKindOfArticle kindOf;
}TArticle;
void readOneArticle(TArticle* arti);
int readMaxArticle(TArticle* arti[]);
void printfOneArticle(TArticle arti);
void printfMaxArticle(TArticle *arti[],int read);
int main()
{
TArticle arti[MAXA];
int howMany;
howMany = readMaxArticle(&arti);
printfMaxArticle(&arti,howMany);
return 0;
}
void readOneArticle(TArticle* arti){
printf("Number: ");
scanf("%d", &(arti->number));
printf("Descrip: ");
scanf("%s", &(arti->description));
printf("SellGrossPrice: ");
scanf("%d",&(arti->sellingGrossPrice));
printf("MinimumStock: ");
scanf("%d",&(arti->minimumStockLevel));
printf("Kind of article (0: Food, 1: Art, 2: Others): ");
scanf("%d",&(arti->kindOf));
if(arti->kindOf == FOOD){
arti->vat= arti->sellingGrossPrice*1.1;
} else if(arti->kindOf == ART){
arti->vat= arti->sellingGrossPrice*1.13;
}else if(arti->kindOf == OTHERS){
arti->vat= arti->sellingGrossPrice*1.2;
}
}
int readMaxArticle(TArticle* arti[]){
int read;
int i=0;
printf("Max Elements (max. 3): ");
scanf("%d",&read);
if(read>MAXA){
printf("Error");
} else{
for(i=0; i<read;i++){
readOneArticle(arti[i]);
printf("\n");
printf("Number: %d\nDescrip.: %s\nSell Gross: %d\nVat: %d\nMin. Stock: %d\n",
(*arti[i]).number,(*arti[i]).description,
(*arti[i]).sellingGrossPrice,(*arti[i]).vat,(*arti[i]).minimumStockLevel);
}
}
return read;
}
void printfOneArticle(TArticle arti){
printf("Number: %d\nDescrip.: %s\nSell Gross: %d\nVat: %d\nMin. Stock: %d\n",
arti.number,arti.description,
arti.sellingGrossPrice,arti.vat,arti.minimumStockLevel);
switch(arti.kindOf){
case 0: printf("Kind: Food\n");
break;
case 1: printf("Kind: Art\n");
break;
case 2: printf("Kind: Others\n");
break;
}
}
void printfMaxArticle(TArticle *arti[],int read){
if(read>MAXA){
} else{
for(int i=0; i<read;i++){
printfOneArticle(*arti[i]);
printf("\n");
}
}
}
You are creating an array of structures: TArticle arti[MAXA];
But when you call readMaxArticle(&arti) function you pass pointer to array of TArticle. TArticle *arti[] it reads like this - arti is array of pointers to TArticle. But you want to pass pointer to array of TArticle. It won't compile anyway.
error: cannot convert 'TArticle (*)[3]' to 'TArticle**'
You might want to look at how to read C declaration and if you want to practice visit this site.

Segmentation Fault Error In C, Function Won't Return Value

I am making a program that searches for a card name and outputs the card's price based on its position in an array. The program finds the card just fine, but the issue is with the spell check, the function should return a value that indicates that the card was not found in the array, but every time a card's name is misspelled, the program outputs "Segmentation Fault" and exits.
#include <cs50.h>
#include <string.h>
#include <stdio.h>
#define COUNTOF(x) (sizeof(x) / sizeof( (x)[0]))
int search_1(string card);
string cards_1[5] = {"card1", "card2", "card3", "card4", "card5"};
int main(void){
string card1 = get_string("Card Purchased: ");
if(search_1(card1) == -1){
printf("CARD NOT FOUND\n");
}
else{
printf("CARD FOUND\n");
}
}
int search_1(string card){
int x = 0;
while(x < COUNTOF(cards_1)){
int cardcmp = strcmp(cards_1[x], card);
if(cardcmp == 0){
return x;
}
x++;
}
return -1;
}

C Void ending issue

I am trying to write a simple parking arrangement code, I want to sort the capacity by 1000 vehicles, color, license plate and model
#include <stdio.h>
#include <stdlib.h>
void NewCar()
{
char model[1000][20];
char color [1000][20];
char number[1000][20];
int x = 1;
printf("\nModel: ");
scanf("%s",model[x]);
printf("Color: ");
scanf("%s",color[x]);
printf("Number: ");
scanf("%s",number[x]);
}
void CarList()
{
int x;
char model[1000][20];
char color [1000][20];
char number[1000][20];
for (x ; x >= 1 ; x--)
{
printf("\n%d. Car: %s %s %s",x,number[x],model[x],color[x]);
}
}
int main()
{
char model[1000][20];
char color [1000][20];
char number[1000][20];
char menu;
int x = 1;
flag:
printf("New Car(N)\nCar List(L)\n");
scanf("%s",&menu);
if (menu == "n" || menu == "N")
{
NewCar();
goto flag;
}
if (menu == "l" || menu == "L")
{
CarList();
goto flag;
}
}
when i don't use void, the code works but i have to use void
Example of the output I want;
1. Car Red Jeep FGX9425
2. Car Yellow Truck OKT2637
3. Car Green Sedan ADG4567
....
This is prefaced by my top comments.
Never use goto. Use (e.g.) a while loop.
Your scanf for menu would [probably] overflow.
As others have mentioned, a number of bugs.
I've refactored your code with your old code and some new code. This still needs more error checking and can be generalized a bit more, but, I've tested it for basic functionality:
#include <stdio.h>
#include <stdlib.h>
// description of a car
struct car {
char model[20];
char color[20];
char number[20];
};
int
NewCar(struct car *cars,int carcount)
{
struct car *car = &cars[carcount];
printf("\nModel: ");
scanf("%s", car->model);
printf("\nColor: ");
scanf("%s", car->color);
printf("\nNumber: ");
scanf("%s", car->number);
++carcount;
return carcount;
}
void
CarList(struct car *cars,int carcount)
{
struct car *car;
int caridx;
for (caridx = 0; caridx < carcount; ++caridx) {
car = &cars[caridx];
printf("%d. Car: %s %s %s\n",
caridx + 1, car->number, car->model, car->color);
}
}
int
main(int argc,char **argv)
{
#if 1
int carcount = 0;
struct car carlist[1000];
#endif
#if 0
char menu;
int x = 1;
#else
char menu[20];
#endif
// force out prompts
setbuf(stdout,NULL);
while (1) {
printf("New Car(N)\nCar List(L)\n");
#if 0
scanf("%s", &menu);
#else
scanf(" %s", menu);
#endif
// stop program
if ((menu[0] == 'q') || (menu[0] == 'Q'))
break;
switch (menu[0]) {
case 'n':
case 'N':
carcount = NewCar(carlist,carcount);
break;
case 'l':
case 'L':
CarList(carlist,carcount);
break;
}
}
return 0;
}
UPDATE:
As you said, there are a few minor errors, it's not a problem for me, but I can write errors if you want to know and fix them.(if you write the plate with a space between it, the code repeats the "new car car list" command many times)
Okay, I've produced an enhanced version that replaces the scanf with a function askfor that uses fgets. The latter will prevent [accidental] buffer overflow. And, mixing scanf and fgets can be problematic. Personally, I always "roll my own" using fgets as it can provide finer grain control [if used with wrapper functions, such as the askfor provided here]
Edit: Per chux, I've replaced the strlen for removing newline with a safer version that uses strchr:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STRMAX 20
// description of a car
struct car {
char model[STRMAX];
char color[STRMAX];
char number[STRMAX];
};
// askfor -- ask user for something
void
askfor(const char *tag,char *ptr)
{
printf("Enter %s: ",tag);
fflush(stdout);
fgets(ptr,STRMAX,stdin);
// point to last char in buffer
// remove newline
#if 0
ptr += strlen(ptr);
--ptr;
if (*ptr == '\n')
*ptr = 0;
#else
// remove trailing newline [if it exists]
ptr = strchr(ptr,'\n');
if (ptr != NULL)
*ptr = 0;
#endif
}
int
NewCar(struct car *cars,int carcount)
{
struct car *car = &cars[carcount];
askfor("Model",car->model);
askfor("Color",car->color);
askfor("Number",car->number);
++carcount;
return carcount;
}
void
CarList(struct car *cars,int carcount)
{
struct car *car;
int caridx;
for (caridx = 0; caridx < carcount; ++caridx) {
car = &cars[caridx];
printf("%d. Car: %s %s %s\n",
caridx + 1, car->number, car->model, car->color);
}
}
int
main(int argc,char **argv)
{
int carcount = 0;
struct car carlist[1000];
char menu[STRMAX];
// force out prompts
setbuf(stdout,NULL);
while (1) {
askfor("\nNew Car(N)\nCar List(L)",menu);
// stop program
if ((menu[0] == 'q') || (menu[0] == 'Q'))
break;
switch (menu[0]) {
case 'n':
case 'N':
carcount = NewCar(carlist,carcount);
break;
case 'l':
case 'L':
CarList(carlist,carcount);
break;
}
}
return 0;
}
UPDATE #2:
Thank you for your bug fix, but as I said in my question, I have to do the "New car" feature using void. You did it with int, can you do it with void?
Okay. When you said "using void", what you meant wasn't totally clear to me [or some others]. There were enough bugs that they overshadowed some other considerations.
So, I have to assume that "using void" means that the functions return void.
Your original functions were defined as void NewCar() and void CarList(). Those could not have done the job as is, so they had to be changed.
If you have similar criteria, a better way to phrase that would be:
I must create two functions, with the following function signatures ...
Anyway, here's the updated code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STRMAX 20
// description of a car
struct car {
char model[STRMAX];
char color[STRMAX];
char number[STRMAX];
};
// askfor -- ask user for something
void
askfor(const char *tag,char *ptr)
{
printf("Enter %s: ",tag);
fflush(stdout);
fgets(ptr,STRMAX,stdin);
// remove trailing newline [if it exists]
ptr = strchr(ptr,'\n');
if (ptr != NULL)
*ptr = 0;
}
void
NewCar(struct car *cars,int *countptr)
{
int carcount = *countptr;
struct car *car = &cars[carcount];
askfor("Model",car->model);
askfor("Color",car->color);
askfor("Number",car->number);
carcount += 1;
*countptr = carcount;
}
void
CarList(struct car *cars,int carcount)
{
struct car *car;
int caridx;
for (caridx = 0; caridx < carcount; ++caridx) {
car = &cars[caridx];
printf("%d. Car: %s %s %s\n",
caridx + 1, car->number, car->model, car->color);
}
}
int
main(int argc,char **argv)
{
int carcount = 0;
struct car carlist[1000];
char menu[STRMAX];
// force out prompts
setbuf(stdout,NULL);
while (1) {
askfor("\nNew Car(N)\nCar List(L)",menu);
// stop program
if ((menu[0] == 'q') || (menu[0] == 'Q'))
break;
switch (menu[0]) {
case 'n':
case 'N':
#if 0
carcount = NewCar(carlist,carcount);
#else
NewCar(carlist,&carcount);
#endif
break;
case 'l':
case 'L':
CarList(carlist,carcount);
break;
}
}
return 0;
}
However, given your original functions, it may be possible that the signatures have to be: void NewCar(void) and void CarList(void) and that the car list variables must be global scope.
This would be a less flexible and desirable way to do things, but here is a version that uses only global variables for the lists:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STRMAX 20
// description of a car
struct car {
char model[STRMAX];
char color[STRMAX];
char number[STRMAX];
};
#if 1
int carcount = 0;
struct car carlist[1000];
#endif
// askfor -- ask user for something
void
askfor(const char *tag,char *ptr)
{
printf("Enter %s: ",tag);
fflush(stdout);
fgets(ptr,STRMAX,stdin);
// remove trailing newline [if it exists]
ptr = strchr(ptr,'\n');
if (ptr != NULL)
*ptr = 0;
}
void
NewCar(void)
{
struct car *car = &carlist[carcount];
askfor("Model",car->model);
askfor("Color",car->color);
askfor("Number",car->number);
carcount += 1;
}
void
CarList(void)
{
struct car *car;
int caridx;
for (caridx = 0; caridx < carcount; ++caridx) {
car = &carlist[caridx];
printf("%d. Car: %s %s %s\n",
caridx + 1, car->number, car->model, car->color);
}
}
int
main(int argc,char **argv)
{
#if 0
int carcount = 0;
struct car carlist[1000];
#endif
char menu[STRMAX];
// force out prompts
setbuf(stdout,NULL);
while (1) {
askfor("\nNew Car(N)\nCar List(L)",menu);
// stop program
if ((menu[0] == 'q') || (menu[0] == 'Q'))
break;
switch (menu[0]) {
case 'n':
case 'N':
#if 0
carcount = NewCar(carlist,carcount);
#else
NewCar();
#endif
break;
case 'l':
case 'L':
#if 0
CarList(carlist,carcount);
#else
CarList();
#endif
break;
}
}
return 0;
}

Dynamic memory in C with struct

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;
}
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.

Resources