• Need help with C (structures)

    WinMan Member

    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;
    
  • codewithc
    CWC Keymaster

    For one, if “temp” is a struct object, and not a pointer, then you should write

    scanf("%d", &temp.wd->DayNumber);

    Also, did you allocate memory for the “wd” pointer in temp object?

  • Adelaid Member

    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;
    }
    
  • Gallard Member

    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;
    }
    
    }
    
  • Amit Member

    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;
    }
    
Viewing 4 reply threads
  • You must be logged in to reply to this topic.
en_USEnglish