Help With Constant Pointers

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!
 
M

Michael Tsang

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

crystal said:
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)
Writing an operator- instead of function is much better.
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!

If running "t1->hours * 3600" gets a segmentation fault, your t1 does not
point to a valid object.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAkr31iMACgkQG6NzcAXitM+BnQCfcdDzb6YwXaVjvjFKMymroCRa
FUgAnRtuoLxA2jabmuYEglTq9ASuGOb1
=EpTL
-----END PGP SIGNATURE-----
 
I

Ian Collins

crystal said:
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.

No, you are probably dereferencing a null pointer.
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

You don't.
int var = t1->minutes;
int newVar = var *3600;

Don't concern yourself with such minor premature optimisations! The
compiler will take care of them for you.
 
J

Jorgen Grahn

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)

Wasn't this discussed in another thread created by you recently? It
helps the reader if you continue in the same thread (perhaps changing
the subject) rather than creating a new one, with no reference back to
the original thread.

/Jorgen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top