#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;
}
I keep getting this error at almost all occurings of appointmentT and appointmentT_ptr
> ERROR:expected expression before ‘appointmentT"
3 Answers 3
typedef is used to declare type alias. You don't use it when you're declaring variables, so it should be:
struct appointmentT *appointmentT_ptr;
struct appointmentT *appointmentT_ptr2;
2 Comments
The problem lies in the typedef.
I would suggest to move the struct outside main. The typedef would be of the form:
typedef term1 term2
where the term1 will be a synonym of term2.
So this:
typedef struct appointmentT *appointmentT_ptr_t;
will say that appointmentT_ptr_t is a synonum for struct appointmentT *.
Now one would declare a ptr1 and ptr2 for your case.
You should receive a warning like this:
format ‘%s’ expects argument of type ‘char *’, but argument 4 has type ‘char (*)[30]
if not enable your compiler warning (-Wall flag would be nice).
For example this:
printf("%s : %d:%d", &(*appointmentT_ptr1).description, dif_hours, dif_mins);
should be this:
printf("%s : %d:%d", (*appointmentT_ptr1).description, dif_hours, dif_mins);
Moreover this:
(*appointmentT_ptr1).description
is equivalent to this:
appointmentT_ptr1->description
The same goes for the scanf()'s you have.
Also you missed the second argument of main(), which should be argv, not argc.
And don't cast what malloc returns.
Putting them all together you would get something like this:
#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;
}
Future work:
It would be nice to pack things in functions, like in my example here.
Comments
In these statemente
appointmentT_ptr=(struct appointmentT*)malloc(sizeof(struct appointmentT));
appointmentT_ptr2=(struct appointmentT*)malloc(sizeof(struct appointmentT));
appointmentT_ptr and appointmentT_ptr2 are type name but you need to define objects of these types something like
appointmentT_ptr ptr = (struct appointmentT*)malloc(sizeof(struct appointmentT));
appointmentT_ptr2 ptr2 = (struct appointmentT*)malloc(sizeof(struct appointmentT));
and use identifiers ptr and ptr2 everywhere where the objects are used.
Comments
Explore related questions
See similar questions with these tags.