Skip to contentSkip to conversation

can't initialize a constant using another constant?

DISCUSSION Shriramana Sharma 3 participants 6 messages Feb 26, 2008 – Feb 28, 2008

lore
Share & tools
lore
#0 lore raw

Dear list,

Quite some time since I wrote. I hope you are all keeping well. :)

A strange thing I ran across this evening. I was not allowed to do:

const double lunarSynodicMonth = 29.53059 ;
const double daysLunarPhaseAngleTakesPerDegree = lunarSynodicMonth / 360.0 ;

I got the following error:

wdf-karanas.c:17: error: initializer element is not constant
make: *** [wdf-karanas.o] Error 1

I would be grateful if anyone could shed some light. TIA.

Shriramana Sharma.

#1 lore raw

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

Quoted 9 lines · Shriramana Sharma

Dear list,

Quite some time since I wrote. I hope you are all keeping well. :)

A strange thing I ran across this evening. I was not allowed to do:

const double lunarSynodicMonth = 29.53059 ;
const double daysLunarPhaseAngleTakesPerDegree = lunarSynodicMonth / 360.0 ;

Quoted 6 lines

I got the following error:

wdf-karanas.c:17: error: initializer element is not constant
make: *** [wdf-karanas.o] Error 1

I would be grateful if anyone could shed some light. TIA.

What compiler are you using?
I did a quick test on g++ 4.1.2 (x86_64) and it seemed to be accepted without warning.

Kristof
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFHxRrVUEZ9DhGwDugRAjZBAJ9pl0mGAJHPGbwePR0DVXoVh4kRzwCgnTn2 zzdeiILqi1JZ9M9VbUBewr8=
=KWWv
-----END PGP SIGNATURE-----

#2 lore raw

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

Quoted 8 lines · Kristof Provost
Quoted 7 lines · Shriramana Sharma

A strange thing I ran across this evening. I was not allowed to do:

const double lunarSynodicMonth = 29.53059 ;
const double daysLunarPhaseAngleTakesPerDegree = lunarSynodicMonth / 360.0 ;

I got the following error:

Quoted 7 lines
Quoted 4 lines

wdf-karanas.c:17: error: initializer element is not constant
make: *** [wdf-karanas.o] Error 1

I would be grateful if anyone could shed some light. TIA.

What compiler are you using?
I did a quick test on g++ 4.1.2 (x86_64) and it seemed to be accepted without warning.

I took a better look. I get the same error with gcc.

It looks like this is allowed in C++ and not in plain C (or at least, g++ allows it and gcc doesn't).

Kristof
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFHxSBIUEZ9DhGwDugRAkCiAJoCe+3665vkuJCWxdJTNsu/CxAAzgCePlm4 0OTsa8DQNj/CCG2eKiFrXp4=
=9GTz
-----END PGP SIGNATURE-----

#3 lore raw
In reply to Shriramana Sharma
Quoted 7 lines · Shriramana Sharma

Quite some time since I wrote. I hope you are all keeping well. :)

A strange thing I ran across this evening. I was not allowed to do:

const double lunarSynodicMonth = 29.53059 ;
const double daysLunarPhaseAngleTakesPerDegree = lunarSynodicMonth / 360.0 ;

Quoted 6 lines

I got the following error:

wdf-karanas.c:17: error: initializer element is not constant
make: *** [wdf-karanas.o] Error 1

I would be grateful if anyone could shed some light. TIA.

In C, "const" is only relevant to pointer targets. Adding the "const" modifier to a variable has no effect.

You need to change lunarSynodicMonth to a macro (#define) if you want to be able to use it in an initialiser.

Signature · 1 line
Glynn Clements <[email protected]>
#4 lore raw

Thanks to all those who replied. I am very sorry I did not specify the compiler version etc. I should have. It's gcc version 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2).

Quoted 2 lines · Glynn Clements

In C, "const" is only relevant to pointer targets. Adding the "const" modifier to a variable has no effect.

I don't understand what you mean. I just tried gcc -o foo foo.c on:

# include <stdio.h>
main () {
        const int i = 1 ;
        i = 2 ;
        printf ( "%d\n", i ) ;
}

and I got:

foo.c: In function ‘main’:
foo.c:6: error: assignment of read-only variable ‘i’

So in what sense are you saying adding const to a variable has no effect?

Shriramana Sharma.
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#5 lore raw
Quoted 17 lines · Shriramana Sharma

Thanks to all those who replied. I am very sorry I did not specify the compiler version etc. I should have. It's gcc version 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2).

Quoted 2 lines · Glynn Clements

In C, "const" is only relevant to pointer targets. Adding the "const" modifier to a variable has no effect.

I don't understand what you mean. I just tried gcc -o foo foo.c on:

# include <stdio.h>
main () {
         const int i = 1 ;
         i = 2 ;
         printf ( "%d\n", i ) ;
}
Quoted 6 lines

and I got:

foo.c: In function �main�:
foo.c:6: error: assignment of read-only variable �i�

So in what sense are you saying adding const to a variable has no effect?

Sorry; my mistake.

However, although the compiler will prevent modification to const objects (and if the compiler didn't, the CPU will, as they are stored in the .rodata section, which is mapped read-only), they are still considered variables rather than (compile-time) constants, and can't be used in initialisers or in array dimensions.

In C++, const-qualified objects are treated as compile-time constants.

Signature · 5 lines
Glynn Clements <[email protected]>
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html