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

I keep getting this error at almost all occurings of appointmentT and appointmentT_ptr

> ERROR:expected expression before ‘appointmentT"
i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
Chrisa4
  • 39
  • 9

3 Answers3

1

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;
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • @Chrisa4 if you like the answer, you should accept it, so that the question appears as answered in the question feed. Moreover you give the man that helped you something in return, which is nice. I would suggest you to take a look at my answer too, since your code has more bugs. – gsamaras Nov 08 '14 at 17:35
  • @G.Samaras Sorry you 're right> Im new here and I didn't know how it works – Chrisa4 Nov 12 '14 at 15:25
1

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.

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
0

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335