The Way to Programming
The Way to Programming
can anyone see the problem in this line ?
scanf("%d",&temp->wd->DayNumber);
temp is a structure i get an error when i write it this way. thank you !
typedef struct { int DayNumber; //1-31 int WorkHours; // 1-12 } WorkDay; typedef struct { char name[30]; long ID;//. int workdays; // WorkDay * wd; / double salary; / } Worker;
you really have made this as difficult to answer as you possibly could. When asking questions like this, you need to provide your code. Anyway, see if this explains some of your problems:
#include#include typedef struct { int DayNumber; int WorkHours; } WorkDay; typedef struct { char name[30]; long ID; int workdays; WorkDay * wd; double salary; } Worker; Worker *newWorker() { Worker *w = malloc(sizeof(Worker)); w->wd = malloc(sizeof(WorkDay)); return w; } int main() { Worker *temp = newWorker(); printf("What day?"); scanf("%d",&(temp->wd->DayNumber)); printf("Day supplied = %d\n", temp->wd->DayNumber); free(temp->wd); free(temp); return 0; }
This is my function : at first factory is set to NULL and size to 0
Worker * add_workers(Worker * factory, int * size){ Worker **arr,*temp; int i,length; printf("Enter number of workers for adding : \n"); scanf("%d",&length); getchar(); arr=(Worker**)malloc(length*sizeof(Worker*)); size=&length; for(i=0;iname); printf("Enter ID :\n"); scanf("%ld",&temp->ID); printf("Enter number day's working in month: \n"); scanf("%d",&temp->workdays); printf("Enter the day number : \n"); scanf("%d",&temp->wd->DayNumber); printf("Enter the day's work hours : \n"); scanf("%d",temp->wd->WorkHours); getchar(); arr[i]=temp; } }
i’ve made some changes according to your suggestions please tell me what do you think ?
Worker * add_workers(Worker * factory, int * size){ Worker **arr,*temp; int i,length; printf("Enter number of workers for adding : \n"); scanf("%d",&length); getchar(); arr=(Worker**)malloc(length*sizeof(Worker*)); size=&length; for(i=0;iwd = (WorkDay*)malloc(sizeof(WorkDay)); printf("Enter the Worker name :\n"); gets(w->name); printf("Enter ID :\n"); scanf("%ld",&w->ID); printf("Enter number day's working in month: \n"); scanf("%d",&w->workdays); w->wd = (WorkDay*)malloc(sizeof(WorkDay)); printf("Enter the day number : \n"); scanf("%d",&(w->wd->DayNumber)); printf("Enter the day's work hours : \n"); scanf("%d",&(w->wd->WorkHours)); return w; }
Sign in to your account