The Daily Click ::. Forums ::. General Chat ::. C Question, the 'const' keyword
 

Post Reply  Post Oekaki 
 

Posted By Message

Aali [Crazy_Productions]



Registered
  13/10/2002
Points
  843
2nd September, 2003 at 13:10:00 -

i was just wondering:

1. whats the point of it? (except it can help debugging abit) where is it really needed?

2. if you create a pointer like this:
'
char const *
'
will the pointer, and not the char become constant?

(maybe this is the wrong place to ask, but whatever..)

 
"If Darl McBride was in charge, he'd probably make marriage unconstitutional too, since clearly it de-emphasizes the commercial nature of normal human interaction, and probably is a major impediment to the commercial growth of prostitution."
-- Linus Torvalds, December 5th 2003.

(Darl McBride is CEO of The SCO Group)


this place sucks but don't tell anyone, it's our little secret, ok?

ChrisB

Crazy?

Registered
  16/08/2002
Points
  5457
2nd September, 2003 at 14:08:53 -

const lets you create constants, so that you don't accidentally change things.
char const * means the pointer is constant, but the data it points to can change. const char * means the pointer points to a const char. (e.g. string literals)

 
n/a

Aali [Crazy_Productions]



Registered
  13/10/2002
Points
  843
2nd September, 2003 at 14:12:40 -

just as i thought then... thanks for making it clear to me

 
"If Darl McBride was in charge, he'd probably make marriage unconstitutional too, since clearly it de-emphasizes the commercial nature of normal human interaction, and probably is a major impediment to the commercial growth of prostitution."
-- Linus Torvalds, December 5th 2003.

(Darl McBride is CEO of The SCO Group)


this place sucks but don't tell anyone, it's our little secret, ok?

Kris

Possibly Insane

Registered
  17/05/2002
Points
  2017
2nd September, 2003 at 14:20:19 -

Using constants instead of 'variables that don't change' should be faster, too

 
"Say you're hanging from a huge cliff at the top of mt. everest and a guy comes along and says he'll save you, and proceeds to throw religious pamphlets at you while simultaniously giving a sermon." - Dustin G

ShadowCaster

Possibly Insane

Registered
  02/01/2002
Points
  2203
3rd September, 2003 at 05:03:44 -

Works in the same way as the #define keyword used by the pre-processor, in that the variable itself doesnt exist when you run the EXE, instead every time the pre-processor sees the keyword it replaces it with the value, and saves that into the EXE instead. Saves in memory and speed.

Mike

 
"Now I guess we're... 'Path-E-Tech Management'" -Dilbert

Galaxy613



Registered
  29/01/2003
Points
  1765
8th September, 2003 at 18:28:29 -

so why don't you just use #define instead of messing around with const?

 
Image
My forum: http://subsoap.com/ck/forums/index.php

Aali [Crazy_Productions]



Registered
  13/10/2002
Points
  843
9th September, 2003 at 00:51:44 -

because then you can't specify the type of the constant

 
"If Darl McBride was in charge, he'd probably make marriage unconstitutional too, since clearly it de-emphasizes the commercial nature of normal human interaction, and probably is a major impediment to the commercial growth of prostitution."
-- Linus Torvalds, December 5th 2003.

(Darl McBride is CEO of The SCO Group)


this place sucks but don't tell anyone, it's our little secret, ok?
   

Post Reply



 



Advertisement

Worth A Click