PDA

View Full Version : const and static


Strike
06-27-2002, 06:49 AM
I was looking over some GNOME devel docs today, and they praised the importance of learning correct usage of these two keywords in C. I've used them before, but in a very limited capacity, and usually at a professor's urging rather than my own design decision. So, I never really got a grasp as to what each really indicates.

const, I've found out, confuses me most. I can make a char * that isn't truly constant, and the compiler won't bitch. Example:

char * foo(void)
{
return "foo";
}

int main(void)
{
const char *bar;
bar = (char *)malloc(sizeof(char), 4);
bar = foo();
free(bar);
return 0;
}

This compiles just fine. But if I make the function return a const char * (which it is), and I make bar into a non-const char *. I get this compile warning:

[ddipaolo@hundley ~/CS/C/const]% make const
cc const.c -o const
const.c: In function `main':
const.c:10: warning: assignment discards qualifiers from pointer target type


Why? I don't really know.

As far as static goes, I haven't played with it much. I'm still looking for a good online source to answer this question.

Strike
06-27-2002, 07:00 AM
Well, I've figured a bit of it out. The pointer is probably remaining constant in those cases, because if I try to declar a const char foo[] = "bar"; and then say something like foo = "";, the compiler will (appropriately) bitch at me. What I don't really get is how it affects return values of functions.

Strike
06-27-2002, 07:10 AM
hmm, now I'm confused again by what cdecl and cundecl are telling me:

[ddipaolo@hundley ~]% cundecl
declare foo as const pointer to char;
char * const foo;
declare foo as pointer to const char;
const char *foo;

Seems to me that if it was the pointer remaining constant, then it would be declared the first way according to cundecl.

kmj
06-27-2002, 09:58 AM
I've found the MSDN (gasp!) documentation to be a handy reference when I'm trying to decide where to put const when I want the pointer to be constand, or the data that the pointer is pointing to to be constant.


The C++ language prevents assignments that would allow modification of an object or pointer declared as const. Such assignments would remove the information that the object or pointer was declared with, thereby violating the intent of the original declaration. Consider the following declarations:

const char cch = 'A';
char ch = 'B';

Given the preceding declarations of two objects (cch, of type const char, and ch, of type char), the following declaration/initializations are valid:

const char *pch1 = &cch;
const char *const pch4 = &cch;
const char *pch5 = &ch;
char *pch6 = &ch;
char *const pch7 = &ch;
const char *const pch8 = &ch;

The following declaration/initializations are erroneous.

char *pch2 = &cch; // Error
char *const pch3 = &cch; // Error

The declaration of pch2 declares a pointer through which a constant object might be modified and is therefore disallowed. The declaration of pch3 specifies that the pointer is constant, not the object; the declaration is disallowed for the same reason the pch2 declaration is disallowed.

The following eight assignments show assigning through pointer and changing of pointer value for the preceding declarations; for now, assume that the initialization was correct for pch1 through pch8.

*pch1 = 'A'; // Error: object declared const
pch1 = &ch; // OK: pointer not declared const
*pch2 = 'A'; // OK: normal pointer
pch2 = &ch; // OK: normal pointer
*pch3 = 'A'; // OK: object not declared const
pch3 = &ch; // Error: pointer declared const
*pch4 = 'A'; // Error: object declared const
pch4 = &ch; // Error: pointer declared const

ThurberMingus
06-27-2002, 10:00 AM
Hmm . . .

I am not familiar with cdecl and cundecl, what are those? I have only had experience with VC++ and limited experience compiling with some version of gcc . . .

You are right that if you declare a pointer to be a const, then it is the pointer that is const and not the thing being pointed at, at least from what I remember (it has been a while).

Let me know what those things are, if you would . . .

Strike
06-27-2002, 10:41 AM
They are ... cool tools :) One interprets C into English, the other interprets English (well, well-formed Englisht statements) into C.

Anyway, I did some reading up on comp.lang.c (surprisngly the FAQ was totally unhelpful), and got it all figured out. Thanks though ;)

file13
06-27-2002, 11:43 AM
in Bruce Eckel's Thinking in C++ he has a whole chapter on const--describes it in both C and C++. he even goes into static.

i'm sure you know the link but just in case:

http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html

BTW: i thought that "const" was not a part of C89???

Strike
06-27-2002, 12:16 PM
afaik, from reading groups.google.com "const" is not only a part of C89, it's a part of C9X. Plus the GNOME docs were saying to use it when appropriate.

stuka
06-27-2002, 12:59 PM
oooh...fun with const! :D That one can be confusing, but the GNOME docs are right - correct usage can make your code much better by letting the compiler prevent a lot of possible screw-ups.

kmj
06-27-2002, 01:15 PM
what irritates me is when functions ask for a char* when all they need is a const char*, that can be a hassle.

strags
07-04-2002, 04:18 PM
Originally posted by Strike
hmm, now I'm confused again by what cdecl and cundecl are telling me:

[ddipaolo@hundley ~]% cundecl
declare foo as const pointer to char;
char * const foo;
declare foo as pointer to const char;
const char *foo;

Seems to me that if it was the pointer remaining constant, then it would be declared the first way according to cundecl.

const char *foo;

This is a pointer to a character that cannot be modified (although the pointer itself can be modified).

char * const foo;

This is a pointer that cannot be modified. It points to a character whose value can be modified.

I think.

Strike
07-04-2002, 11:48 PM
right, that's what it says above, basically. I told cundecl to give me the C statement to declare foo as a const pointer to a char, and that gave

char * const foo

oneinchhard
07-05-2002, 01:38 PM
I haven't seen an answer on statics so I will give it a shot...

statics are pretty cool. They allow you to do stuff to a class without instantiating it. Here's one ex (c#):


public class foo {
public static int x = 0;
public int y = x;
}

public class bar {
int z;
foo.x = 2;

foo objA = new foo();
foo objB = new foo();

z = objA.y + objB.y; //z is 4
}


You can also call static methods without instantiating a class (in fact you cant call statics with an instance of the class):


public class foo {
public void myMethod() {
//Do junk
}
}

public class bar {
foo.myMethod(); //This works

foo objA = new foo();
objA.myMehod() // This doesn't work
}

LonelyKing
07-05-2002, 01:42 PM
Er... shouldn't myMethod in the last part of your example be declared a public static void? I think that's what you were intending.

ThurberMingus
07-05-2002, 02:16 PM
That must be what he was intending. He forgot the keyword 'static; in the foo class.

oneinchhard
07-05-2002, 02:18 PM
ooops, yeah, static... :)

Strike
07-05-2002, 02:27 PM
Yeah, I pretty much understood static, just didn't see why it would be such a big deal that the GGAD would mention it so strongly. Static is something that is in many languages, so I've used it before. const is a C-ism

stuka
07-11-2002, 03:26 PM
Originally posted by oneinchhard

public class foo {
public static int x = 0;
public int y = x;
}

public class bar {
int z;
foo.x = 2;

foo objA = new foo();
foo objB = new foo();

z = objA.y + objB.y; //z is 4
}



Umm...not to nitpick, but that's Java code, not C/C++!:p

Strike
07-14-2002, 06:11 PM
Actually, that could well be C# :)

stuka
07-15-2002, 09:57 AM
Oh, that's right, I forgot - C#, aka Java w/a different name, named like a C-based language.