C
crystal twix
I'm having trouble understanding constant pointers. If my function is
to compute the time difference between two structs of type
struct Time {
int hours, minutes, seconds;
}
and my function is:
Time *TimeDiff(const Time *t1, const Time *t2)
I thought I could create a long int and convert each of the times to
seconds, subtract the times, and convert back. However, I then
realized that doing:
t1->hours*3600 results in a Segmentation Fault because I am trying to
access the pointer in the wrong way. So how do you usually manipulate
constant pointers without declaring a new pointer? It seems odd and
inefficient if I were to create a new variable and set it to that
value and then do the manipulation. Like
int var = t1->minutes;
int newVar = var *3600;
Thanks!
to compute the time difference between two structs of type
struct Time {
int hours, minutes, seconds;
}
and my function is:
Time *TimeDiff(const Time *t1, const Time *t2)
I thought I could create a long int and convert each of the times to
seconds, subtract the times, and convert back. However, I then
realized that doing:
t1->hours*3600 results in a Segmentation Fault because I am trying to
access the pointer in the wrong way. So how do you usually manipulate
constant pointers without declaring a new pointer? It seems odd and
inefficient if I were to create a new variable and set it to that
value and then do the manipulation. Like
int var = t1->minutes;
int newVar = var *3600;
Thanks!