473,626 Members | 3,439 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

const array declaration

Hi guys,

In a program (not my own) I encountered the declaration of a constant
pointer to an array consisting of two other const pointers to arrays.
Not quite sure why they do it so complicated, but is it legal? Most
compilers accept it, but one doesn't recognize the rhs as a constant.
What are the requirements for the rhs in the declaration of a const
pointer? Is the following program legal C?

int main(int argc, char *argv[]) {
const char *a[] = {"A"};
const char *b[] = {"B"};
const char **z[] = {a, b}; /* this is the statement in question */
return (0);
}

The error message is
"t.c", line 4: error: initialization: constant expression is expected
for variable: `z'

Thanks in advance,

Herbert
Nov 14 '05 #1
16 41757

"herbertF" <hf******@yahoo .co.uk> wrote in message
news:8d******** *************** ***@posting.goo gle.com...
Hi guys,

In a program (not my own) I encountered the declaration of a constant
pointer to an array consisting of two other const pointers to arrays.
Not quite sure why they do it so complicated, but is it legal? Most
compilers accept it, but one doesn't recognize the rhs as a constant.
What are the requirements for the rhs in the declaration of a const
pointer? Is the following program legal C?

int main(int argc, char *argv[]) {
const char *a[] = {"A"};
const char *b[] = {"B"};
const char **z[] = {a, b}; /* this is the statement in question */
return (0);
}


Your const was in the wrong spot.

int main(int argc, char *argv[]) {
char const *a[] = {"A"};
char const *b[] = {"B"};
char const **z[] = {a, b}; /* this is the statement in question */
return (0);
}

Works just fine.

Tom
Nov 14 '05 #2
herbertF wrote:

Hi guys,

In a program (not my own) I encountered the declaration of a constant
pointer to an array consisting of two other const pointers to arrays.
Not quite sure why they do it so complicated, but is it legal? Most
compilers accept it, but one doesn't recognize the rhs as a constant.
What are the requirements for the rhs in the declaration of a const
pointer? Is the following program legal C?

int main(int argc, char *argv[]) {
const char *a[] = {"A"};
const char *b[] = {"B"};
const char **z[] = {a, b}; /* this is the statement in question */
return (0);
}

The error message is
"t.c", line 4: error: initialization: constant expression is expected
for variable: `z'


The complaining compiler is correct: `a' and `b' are not
constant expressions.
Despite the spelling, `const' is not
"constant."

If you think about it in a wider context, you'll see why
`a' and `b' are not constant. Here's a recursive function
with a similar construct, to help show what's happening:

void func(int x) {
const char *a[] = { "A", "B" };
if (0 <= x && x < 2) {
a[x] = "X";
printf ("func(%d): a[0] = %s, a[1] = %s\n",
x, a[0], a[1]);
func (x + 1);
printf ("func(%d): a[0] = %s, a[1] = %s\n",
x, a[0], a[1]);
}
else {
printf ("func(%d): nothing to do\n", x);
}
}

If you call this function with `func(0)', the output will be

func(0): a[0] = X, a[1] = B
func(1): a[0] = A, a[1] = X
func(2): nothing to do
func(1): a[0] = A, a[1] = X
func(0): a[0] = X, a[1] = B

This shows that two different `a' arrays exist: one in
the outer func(0) invocation, and another in the inner func(1).
You can see that there must be more than one `a[]' because
when the outer func(0) executes `a[0] = "X"' the value of `a[0]'
in the inner func(1) is not affected; likewise when the inner
func(1) sets `a[1] = "X"' it does not change `a[1]' in the
outer func(0). Each `a[]' array comes into existence when its
invocation of func() begins, and ceases to exist when its own
func() returns. Thus, even though `a' is just one identifier,
it designates different array objects at different times and
is therefore not a constant.

If you changed the function to declare the array as

static const char *a[] = { "A", "B" };

you would get a different output altogether:

func(0): a[0] = X, a[1] = B
func(1): a[0] = X, a[1] = X
func(2): nothing to do
func(1): a[0] = X, a[1] = X
func(0): a[0] = X, a[1] = X

In this case there is only one `a[]' array, and any changes
made to it in one func() invocation are seen in the other. The
lifetime of this single `a[]' is no longer tied to the execution
of its containing block; this `a[]' comes into existence before
the program starts executing and continues to exist until the
program terminates. In this case, `a' *is* a constant, because
the identifier refers to just one object for the entire time the
program is running. If the one-and-only-one semantics make sense
for your original program, perhaps the cure is to add the `static'
qualifier.

By the way, the non-complaining compilers are not in error.
A compiler is *permitted* to allow non-constant initializers in
addition to the constant initializers required by the Standard.

--
Er*********@sun .com
Nov 14 '05 #3

"Eric Sosman" <Er*********@su n.com> wrote in message
news:40******** *******@sun.com ...
The complaining compiler is correct: `a' and `b' are not
constant expressions.
Despite the spelling, `const' is not
"constant."


Yes it is. There is just a diff between

const char *varname;

and

char const *varname;

The former means the values in varname[...] are constant. E.g.

varname[0] = 'a';

will produce a warning.

In the latter the actual pointer "varname" is constant so

varname = &somebuf;

will produce a warning while

varname[0] = 'a';

will not.

const char const *varname;

will make both constant.

Tom
Nov 14 '05 #4
Tom St Denis <to*@securescie nce.net> wrote:
"Eric Sosman" <Er*********@su n.com> wrote in message
news:40******** *******@sun.com ...
The complaining compiler is correct: `a' and `b' are not
constant expressions.
Despite the spelling, `const' is not
"constant."

Yes it is. There is just a diff between const char *varname; and char const *varname;
These two forms are absolutely equivalent. For the latter you must
be thinking about something like

char * const varname;
The former means the values in varname[...] are constant. E.g. varname[0] = 'a'; will produce a warning.
.... and invoke undefined behavior since varname is not
pointing at anything, but I digress.
In the latter the actual pointer "varname" is constant so
Not true. See my comment above.
const char const *varname;


This is exactly equivalent to:

const const char *varname;

Which is obviously wrong. Again, what you obviously meant to write:

const char * const varname;

--
Alex Monjushko (mo*******@hotm ail.com)
Nov 14 '05 #5
herbertF wrote:
In a program (not my own) I encountered the declaration of a constant
pointer to an array consisting of two other const pointers to arrays.
Not quite sure why they do it so complicated, but is it legal? Most
compilers accept it, but one doesn't recognize the rhs as a constant.
What are the requirements for the rhs in the declaration of a const
pointer? Is the following program legal C?

int main(int argc, char *argv[]) {
const char *a[] = {"A"};
const char *b[] = {"B"};
const char **z[] = {a, b}; /* this is the statement in question */
return (0);
}

The error message is
"t.c", line 4: error: initialization:
constant expression is expected for variable: `z'
Let me try:
cat main.c int main(int argc, char* argv[]) {
const char* const a[] = {"A"};
const char* const b[] = {"B"};
const char* const *z[] = {a, b};
return 0;
}
gcc -Wall -std=c99 -pedantic -o main main.c

main.c: In function `main':
main.c:6: warning: unused variable `z'

It seems to work just fine.

Nov 14 '05 #6

"Alex Monjushko" <mo*******@hotm ail.com> wrote in message
news:c0******** *****@ID-190529.news.uni-berlin.de...
Tom St Denis <to*@securescie nce.net> wrote:
"Eric Sosman" <Er*********@su n.com> wrote in message
news:40******** *******@sun.com ...

The complaining compiler is correct: `a' and `b' are not
constant expressions.
Despite the spelling, `const' is not
"constant."

Yes it is. There is just a diff between

const char *varname;

and

char const *varname;


These two forms are absolutely equivalent. For the latter you must
be thinking about something like

char * const varname;


Yeah, oops.

Tom
Nov 14 '05 #7
nrk
Tom St Denis wrote:

"Eric Sosman" <Er*********@su n.com> wrote in message
news:40******** *******@sun.com ...
The complaining compiler is correct: `a' and `b' are not
constant expressions.
Despite the spelling, `const' is not
"constant."
Yes it is. There is just a diff between


Bad Tom!! "Here be dragons" :-)
const char *varname;

and

char const *varname;

The former means the values in varname[...] are constant. E.g.

Nope. Both mean exactly the same thing. Perhaps you wanted to show the
difference between:
const char *varname;
and
char * const varname;

Former is a pointer to a const char, the latter a const pointer to char.
varname[0] = 'a';

will produce a warning.

In the latter the actual pointer "varname" is constant so

varname = &somebuf;

will produce a warning while

varname[0] = 'a';

will not.

const char const *varname;

will make both constant.

Nope. What you're looking for is:
const char * const varname;

Which makes varname a const pointer to const char.

-nrk.
Tom


--
Remove devnull for email
Nov 14 '05 #8
Tom St Denis wrote:

"Eric Sosman" <Er*********@su n.com> wrote in message
news:40******** *******@sun.com ...
The complaining compiler is correct: `a' and `b' are not
constant expressions.
Despite the spelling, `const' is not
"constant."
Yes it is. There is just a diff between

const char *varname;

and

char const *varname;


No, not really. Both mean: "varname is a pointer to const char".

The former means the values in varname[...] are constant. E.g.

varname[0] = 'a';

will produce a warning.

In the latter the actual pointer "varname" is constant so

varname = &somebuf;

will produce a warning while

varname[0] = 'a';

will not.

const char const *varname;

will make both constant.


No.

const char *varname;

and

char const *varname;

both mean the same thing - a pointer to const char. If you want a const
pointer to char, you need:

char * const varname;

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #9
On Mon, 09 Feb 2004 19:10:46 GMT, "Tom St Denis"
<to*@securescie nce.net> wrote in comp.lang.c:

"herbertF" <hf******@yahoo .co.uk> wrote in message
news:8d******** *************** ***@posting.goo gle.com...
Hi guys,

In a program (not my own) I encountered the declaration of a constant
pointer to an array consisting of two other const pointers to arrays.
Not quite sure why they do it so complicated, but is it legal? Most
compilers accept it, but one doesn't recognize the rhs as a constant.
What are the requirements for the rhs in the declaration of a const
pointer? Is the following program legal C?

int main(int argc, char *argv[]) {
const char *a[] = {"A"};
const char *b[] = {"B"};
const char **z[] = {a, b}; /* this is the statement in question */
return (0);
}


Your const was in the wrong spot.

int main(int argc, char *argv[]) {
char const *a[] = {"A"};
char const *b[] = {"B"};
char const **z[] = {a, b}; /* this is the statement in question */
return (0);
}

Works just fine.

Tom


There is no difference at all between "const type" and "type const" in
a declarator.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
13274
by: Victor Hannak | last post by:
I have a class that needs to reference a const array in several of its methods. Where should I put the declaration/initialization of this array so that it is only created once when the class is instantiated, but is visible to all of the methods of only this class ? const unsigned short ConstantVectorWidth = 10; const float ConstantVector = {5,10,15,20,25,30,35,40,45,50}; Does this syntax look correct?
7
2827
by: Michael Klatt | last post by:
Is there any practical difference between a local variable in main() declared 'const' and one declared 'static const'? int main() { static const int i1(0); const int i2(0); return 0; }
7
4352
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have type of "const char *". Right? But why does the compiler I am using allow s to be modified, instead of generating compile error?
2
2493
by: Pavel | last post by:
I am writing software for an embedded application and here is the question. GCC would emit data declared like const char text = "abc"; to .rodata (i.e. "read only data") section. I can put this section to flash memory and that would be OK. I have a structure with one member of it being const char** array;
15
3334
by: candy_init | last post by:
hi, Can anyboby please tell me that why the following code is'nt compiling: int main(void) { const int a=5; int buf; return 0; }
4
21699
by: Bilgehan.Balban | last post by:
Hi, The following code: #include <stdio.h> // const int const_asize = 10; #define define_asize = 10; int array = {1,2,3,4,5,6,7,8,9,0};
0
1867
by: d3x0xr | last post by:
Heh, spelled out in black and white even :) Const is useles... do NOT follow the path of considering any data consatant, because in time, you will have references to it that C does not handle, and you'll be left with just noisy compiler warnings and confusion. if you start a project with all char *, and char ** and even char ***, if you begin at the low level weeding out references of 'passing const char * to char * ( such as...
9
10517
by: Peithon | last post by:
Hi, This is a very simple question but I couldn't find it in your FAQ. I'm using VC++ and compiling a C program, using the /TC flag. I've got a function for comparing two strings int strspcmp(const char * s1, const char * s2) {
10
5949
by: Stephen Howe | last post by:
Hi Just going over some grey areas in my knowledge in C++: 1) If I have const int SomeConst = 1; in a header file, it is global, and it is included in multiple translations units, but it is unused, I know that it does not take up storage in the
0
8268
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8202
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8707
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8641
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8366
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5575
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4202
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2628
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.