2014-11-08 2 views
0
#include <stdio.h> 
#include <stdlib.h> 

int main(int argc, char *argc[]){ 

    struct appointmentT{ 
     int hours; 
     int minutes; 
     char description[30]; 
    }; 

    int dif_hours, dif_mins; 

    typedef struct appointmentT *appointmentT_ptr; 
    typedef struct appointmentT *appointmentT_ptr2; 

    appointmentT_ptr=(struct appointmentT*)malloc(sizeof(struct appointmentT)); 
    if(appointmentT_ptr==NULL){ 
     printf("no memory"); 
     return(1); 
    } 

    appointmentT_ptr2=(struct appointmentT*)malloc(sizeof(struct appointmentT)); 
    if(appointmentT_ptr2==NULL){ 
     printf("no memory"); 
     return(1); 
    } 

    printf("Enter first appointment's info:\n"); 
    scanf("%d:%d %s", &(*appointmentT_ptr).hours, &(*appointmentT_ptr).minutes, &(*appointmentT_ptr).description); 

    printf("Enter second appointment's info:\n"); 
    scanf("%d:%d %s", &(*appointmentT_ptr2).hours, &(*appointmentT_ptr2).minutes, &(*appointmentT_ptr2).description); 

    dif_mins=(*appointmentT_ptr).minutes-(*appointmentT_ptr2).minutes; 
    dif_hours=(*appointmentT_ptr).hours-(*appointmentT_ptr2).hours; 

    if(dif_mins<0){ 
     dif_hours--; 
     dif_mins=60-dif_mins; 
    } 

    printf("%s : %d:%d",&(*appointmentT_ptr).description, dif_hours, dif_mins); 

    free(appointmentT_ptr); 
    free(appointmentT_ptr2); 

    return 0; 
} 

я получаю эту ошибку при почти всех occurings из appointmentT и appointmentT_ptrуказателей на структуры с таНос-выражением ожидался ошибка C

> ERROR:expected expression before ‘appointmentT" 

ответ

1

typedef используется для объявления типа псевдонима. Вы не можете использовать его, когда вы объявление переменных, поэтому она должна быть:

struct appointmentT *appointmentT_ptr; 
struct appointmentT *appointmentT_ptr2; 
+0

Это сработало сейчас, спасибо – Chrisa4

+0

@ Chrisa4, если вам нравится ответ, вы должны принять его, чтобы вопрос появился как ответ в фиде вопроса. Кроме того, вы даете человеку, который помог вам что-то взамен, что приятно. Я бы посоветовал вам взглянуть на мой ответ, так как ваш код имеет больше ошибок. – gsamaras

+0

@ G.Samaras Извини, что ты прав> Im новый здесь, и я не знал, как это работает – Chrisa4

1

Проблема заключается в typedef.

Я бы предложил переместить структуру за пределы основного. typedef будет иметь вид:

typedef term1 term2

где term1 будет синонимом term2.

Так что это:

typedef struct appointmentT *appointmentT_ptr_t; 

будет сказать, что appointmentT_ptr_t является synonum для struct appointmentT *.

Теперь можно было бы объявить ptr1 и ptr2 для вашего дела.


Вы должны получить предупреждение, как это:

format ‘%s’ expects argument of type ‘char *’, but argument 4 has type ‘char (*)[30] 

, если не включить ваше предупреждение компилятора (-Wall флаг будет хорошо).

Например это:

printf("%s : %d:%d", &(*appointmentT_ptr1).description, dif_hours, dif_mins);

должно быть следующее:

printf("%s : %d:%d", (*appointmentT_ptr1).description, dif_hours, dif_mins);

Кроме того, это:

(*appointmentT_ptr1).description

эквивалентно это:

appointmentT_ptr1->description

То же самое для scanf() «s у вас есть.


Также вы пропустили второй аргумент main(), который должен быть argv, не argc.

И don't cast what malloc returns.


Собираем их вместе, вы получите что-то вроде этого:

#include <stdio.h> 
#include <stdlib.h> 

struct appointmentT { 
    int hours; 
    int minutes; 
    char description[30]; 
}; 
typedef struct appointmentT *appointmentT_ptr_t; 

int main(int argc, char *argv[]) { 

    appointmentT_ptr_t appointmentT_ptr1, appointmentT_ptr2; 

    int dif_hours, dif_mins; 
    appointmentT_ptr1 = malloc(sizeof(struct appointmentT)); 
    if (appointmentT_ptr1 == NULL) { 
    printf("no memory"); 
    return (1); 
    } 

    appointmentT_ptr2 = malloc(sizeof(struct appointmentT)); 
    if (appointmentT_ptr2 == NULL) { 
    printf("no memory"); 
    return (1); 
    } 

    printf("Enter first appointment's info:\n"); 
    scanf("%d:%d %s", &(appointmentT_ptr1->hours), &(appointmentT_ptr1->minutes), 
     appointmentT_ptr1->description); 

    printf("Enter second appointment's info:\n"); 
    scanf("%d:%d %s", &(appointmentT_ptr2->hours), &(appointmentT_ptr2->minutes), 
     appointmentT_ptr2->description); 

    dif_mins = appointmentT_ptr1->minutes - appointmentT_ptr2->minutes; 
    dif_hours = appointmentT_ptr1->hours - appointmentT_ptr2->hours; 

    if (dif_mins < 0) { 
    dif_hours--; 
    dif_mins = 60 - dif_mins; 
    } 

    printf("%s : %d:%d", appointmentT_ptr1->description, dif_hours, dif_mins); 

    free(appointmentT_ptr1); 
    free(appointmentT_ptr2); 

    return 0; 
} 

Будущая работа:

Было бы хорошо, чтобы упаковать вещи в функциях, как в моем примере here.

0

В этом statemente

appointmentT_ptr=(struct appointmentT*)malloc(sizeof(struct appointmentT)); 
appointmentT_ptr2=(struct appointmentT*)malloc(sizeof(struct appointmentT)); 

appointmentT_ptr и appointmentT_ptr2 являются именем типа, но вам нужно определить объекты этих типов что-то вроде

appointmentT_ptr ptr = (struct appointmentT*)malloc(sizeof(struct appointmentT)); 
appointmentT_ptr2 ptr2 = (struct appointmentT*)malloc(sizeof(struct appointmentT)); 

и идентификаторов использования ПТРА и ptr2 везде, где используются объекты ,

Смежные вопросы