t变量从assigntime函数开始出现错误,说它必须有一个指向struct或union类型的指针。指针是我的弱点,如果有人能解释,不只是给我答案,我需要做些什么来解决这个问题最有帮助!欢呼声。
//MY TIME C FILE
#include "my_time.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct my_time_int
{
int hour;
int minute;
int second;
};
void init_my_time(my_time *t)
{
t=(my_time)malloc(sizeof(struct init_my_time*));
}
/*
* Alter hour, minute, and second
* Param h new value for hour
* Param m new value for minute
* Param s new value for second
*/
void assignTime(my_time *t, int h, int m, int s)
{
t->hour = h;
t->minute = m;
t->second = s;
}
//FOLLOWING CODE T VARIABLE HAS RED UNDERLINE ERROR SAYING EXPRESSION MUST HAVE POINTER TO STRUCT OR UNION>
char *toString(my_time t)
{
char *r = (char *)malloc(12 * sizeof(char));
if (t.hour >= 12) {
if (t.hour == 12)
sprintf(r, "%02d:%02d:%02d PM", 12, t.minute, t.second);
else
sprintf(r, "%02d:%02d:%02d PM", t.hour - 12, t.minute, t.second);
}
else {
if (t.hour == 0)
sprintf(r, "%02d:%02d:%02d AM", 12, t.minute, t.second);
else
sprintf(r, "%02d:%02d:%02d AM", t.hour, t.minute, t.second);
}
return r;
}
/*
* Find printable form of time in 24 hour mode
* Return String form of time in 24 hour mode for printing etc.
*/
char *toMilString(my_time t)
{
char *s = (char *)malloc(9 * sizeof(char));
sprintf(s, "%02d:%02d:%02d", t.hour, t.minute, t.second);
return s;
}
/*
* Find number of seconds elapsed since midnight
* Return number of seconds elapsed since midnight as int
*/
int secsSinceMidnight(my_time t)
{
return t.second + (60 * t.minute) + (60 * 60 * t.hour);
}
此处的头文件:
#include <stdbool.h>
struct my_time_int;
typedef struct my_time_int *my_time;
void init_my_time(my_time *t);
void assignTime(my_time *t, int h, int m, int s);
void addTime(my_time t, double s);
char *toString(my_time t);
char *toMilString(my_time t);
bool equals(my_time this, my_time that);
bool my_timeIncHour(my_time *t);
bool my_timeIncMinute(my_time *t);
bool my_timeIncSecond(my_time *t);
答案 0 :(得分:3)
您的代码中存在一些错误。
主要是指针的使用在预期结果方面不正确。在标题中,您有以下行:
typedef struct my_time_int *my_time;
有效地声明my_time
是struct my_time_int
的指针类型。但是在函数的原型(以及定义)中,您使用指向my_time
的指针作为参数:my_time* t
。实际上,您在这里使用指向结构my_time_int的指针。
因此,当您尝试使用deference arrow运算符t
分配给->
时,您会犯一个错误,因为实际上您正在指向指向结构的指针而不指向普通指针到你想要的结构。
您还应该避免在.
类型的变量上使用my_time
运算符,因为它们是事实指针。您应该在它们上使用箭头->
运算符。
这里提出的解决方案:
//MY TIME C FILE
#include "prova.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct my_time_int
{
int hour;
int minute;
int second;
};
//void init_my_time(my_time *t)
my_time init_my_time()
{
//t=(my_time)malloc(sizeof(struct init_my_time*));
return (my_time)malloc(sizeof(struct my_time_int));
}
/*
* Alter hour, minute, and second
* Param h new value for hour
* Param m new value for minute
* Param s new value for second
*/
//void assignTime(my_time *t, int h, int m, int s)
void assignTime(my_time t, int h, int m, int s)
{
t->hour = h;
t->minute = m;
t->second = s;
}
//FOLLOWING CODE T VARIABLE HAS RED UNDERLINE ERROR SAYING EXPRESSION MUST HAVE POINTER TO STRUCT OR UNION>
char *toString(my_time t)
{
char *r = (char *)malloc(12 * sizeof(char));
//if (t.hour >= 12) {
if(t->hour >= 12){
//if (t.hour == 12)
if(t->hour == 12)
//sprintf(r, "%02d:%02d:%02d PM", 12, t.minute, t.second);
sprintf(r, "%02d:%02d:%02d PM", 12, t->minute, t->second);
else
//sprintf(r, "%02d:%02d:%02d PM", t.hour - 12, t.minute, t.second);
sprintf(r, "%02d:%02d:%02d PM", t->hour - 12, t->minute, t->second);
}
else {
//if (t.hour == 0)
if (t->hour == 0)
//sprintf(r, "%02d:%02d:%02d AM", 12, t.minute, t.second);
sprintf(r, "%02d:%02d:%02d AM", 12, t->minute, t->second);
else
//sprintf(r, "%02d:%02d:%02d AM", t.hour, t.minute, t.second);
sprintf(r, "%02d:%02d:%02d AM", t->hour, t->minute, t->second);
}
return r;
}
/*
* Find printable form of time in 24 hour mode
* Return String form of time in 24 hour mode for printing etc.
*/
char *toMilString(my_time t)
{
char *s = (char *)malloc(9 * sizeof(char));
//sprintf(s, "%02d:%02d:%02d", t.hour, t.minute, t.second);
sprintf(s, "%02d:%02d:%02d", t->hour, t->minute, t->second);
return s;
}
/*
* Find number of seconds elapsed since midnight
* Return number of seconds elapsed since midnight as int
*/
int secsSinceMidnight(my_time t)
{
//return t.second + (60 * t.minute) + (60 * 60 * t.hour);
return t->second + (60 * t->minute) + (60 * 60 * t->hour);
}
标题也是:
#include <stdbool.h>
struct my_time_int;
typedef struct my_time_int *my_time;
//void init_my_time(my_time *t);
my_time init_my_time();
//void assignTime(my_time *t, int h, int m, int s);
void assignTime(my_time t, int h, int m, int s);
//and son on removing the unnecessary pointer types
void addTime(my_time t, double s);
char *toString(my_time t);
char *toMilString(my_time t);
bool equals(my_time this, my_time that);
bool my_timeIncHour(my_time t);
bool my_timeIncMinute(my_time t);
bool my_timeIncSecond(my_time t);
您可以在注释代码中查看以前的错误声明和定义。
修改强>
正如评论中所指出的,init_my_time
如定义的那样会泄漏内存,因为它会分配一个它不会返回给调用者的指针。这里正确的做法是分配内存并将指向该内存的指针返回给调用者。这需要更改init_my_time
的声明和定义,如上面在代码中所做的那样。
答案 1 :(得分:0)
尝试使用i
而不是malloc(sizeof(struct my_time_int));
您正在为指向malloc(sizeof(struct init_my_time*));
而不是init_my_time
的指针分配内存。
my_time
是指向t
指针的指针,这意味着您需要分配足够的内存来存储my_time_int
对象。指针本身存储在堆栈中,因此您无需为其分配内存。
my_time_int