A
arnuld
I have declared an int as const but compiler still says that it is not a
const:
include <stdio.h>
#include <stdlib.h>
int main()
{
const int MAXSIZE = 100;
char ac[MAXSIZE] = "abc";
char *pc;
for( pc = ac; *pc != '\0'; ++pc )
{
printf("%c\n", *pc);
}
return EXIT_SUCCESS;
}
============ OUTPUT ==============
[arnuld@raj C]$ gcc -ansi -pedantic -Wall -Wextra test.c
test.c: In function `main':
test.c:8: warning: ISO C90 forbids variable-size array `ac'
test.c:8: error: variable-sized object may not be initialized
[arnuld@raj C]$
K&R2 section 2.4, says:
"For an array, the const qualifier says that elements will not be
altered."
but I can't even compile the program.
[arnuld@raj C]$ gcc --version
gcc (GCC) 3.4.3 20041212 (Red Hat 3.4.3-9.EL4)
Copyright (C) 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is
NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.
const:
include <stdio.h>
#include <stdlib.h>
int main()
{
const int MAXSIZE = 100;
char ac[MAXSIZE] = "abc";
char *pc;
for( pc = ac; *pc != '\0'; ++pc )
{
printf("%c\n", *pc);
}
return EXIT_SUCCESS;
}
============ OUTPUT ==============
[arnuld@raj C]$ gcc -ansi -pedantic -Wall -Wextra test.c
test.c: In function `main':
test.c:8: warning: ISO C90 forbids variable-size array `ac'
test.c:8: error: variable-sized object may not be initialized
[arnuld@raj C]$
K&R2 section 2.4, says:
"For an array, the const qualifier says that elements will not be
altered."
but I can't even compile the program.
[arnuld@raj C]$ gcc --version
gcc (GCC) 3.4.3 20041212 (Red Hat 3.4.3-9.EL4)
Copyright (C) 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is
NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.