c - Expression must have pointer to struct or union error -
t variable coming error assigntime function onwards saying must have pointer struct or union type. pointers weakness, if explain, not give me answer, need fix helpful! cheers.
//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, , second * param h new value hour * param m new value minute * param s new value 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 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 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 int */ int secssincemidnight(my_time t) { return t.second + (60 * t.minute) + (60 * 60 * t.hour); }
header file here:
#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);
there couple of errors in code.
primarily use of pointers it's not correct in respect desired outcome. in header have line:
typedef struct my_time_int *my_time;
which declares my_time
being pointer type struct my_time_int
. in function's prototypes (and definitions too) use pointer my_time
argument: my_time* t
. in fact here using pointer pointer struct my_time_int.
so when try assign t
using deference arrow operator ->
make mistake, because in fact assigning pointer pointer struct not plain pointer struct wish.
you should avoid using .
operator on variables of type my_time
because in facts pointers. should use instead arrow ->
operator on them.
here proposed solution:
//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, , second * param h new value hour * param m new value minute * param s new value 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 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 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 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); }
and header too:
#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 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);
as can se in commented code there previous erroneous declarations , definitions.
edit
as pointed in comments, init_my_time
, defined, leaks memory because allocates pointer doesn't return caller. right thing here allocate memory , return pointer memory caller. requires changing declaration , definition of init_my_time
done above in code.
Comments
Post a Comment