指向具有 malloc 表达式预期错误 C 的结构的指针


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

我几乎在所有appointmentTappointmentT_ptr中都收到此错误

> ERROR:expected expression before ‘appointmentT"
typedef用于

声明类型别名。声明变量时不使用它,因此它应该是:

struct appointmentT *appointmentT_ptr;
struct appointmentT *appointmentT_ptr2;

问题出在typedef .

我建议将结构移到主结构之外。typedef的形式如下:

typedef term1 term2

其中term1将是term2的同义词。

所以这个:

typedef struct appointmentT *appointmentT_ptr_t;

会说appointmentT_ptr_tstruct 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()也是如此。


你也错过了main()的第二个参数,它应该是argv的,而不是argc的。

并且不要施放马洛克返回的内容。


将它们放在一起,你会得到这样的结果:

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

今后的工作:

将东西打包在函数中会很好,就像我在这里的例子一样。

在这些语句中

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

appointmentT_ptrappointmentT_ptr2是类型名称,但您需要定义这些类型的对象,例如

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

并在使用对象的所有位置使用标识符 PTR 和 PTR2。

相关内容

  • 没有找到相关文章

最新更新