Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > extern const char * vs. extern const char []http://tinyurl.com/47e3k

Reply
Thread Tools

extern const char * vs. extern const char []http://tinyurl.com/47e3k

 
 
Thomas Matthews
Guest
Posts: n/a
 
      07-31-2004
Hi,

I'm puzzled over the difference between:
extern const char *
and
extern const char [].
My understanding is that the two are equivalent,
but the program below shows elsewise.

Given the simple program below:
// File names.cpp
extern const char Hello_Text[] = "Hello";
extern const char World_Text[] = "World!";

// File main.cpp
#include <iostream>
#include <cstdlib>
using std::cout;
using std::endl;

template <const char * name>
void print_text(void) // personal pref. here
{
cout << name << endl;
return;
}

/* Refer to variables in names.cpp */
/* Note declaration of char * here */
/* versus char [] in names.cpp. */
extern const char * Hello_Text;
extern const char * World_Text;

int main(void)
{
cout << "Start of Test" << endl;
print_text<Hello_Text>();
print_text<World_Text>();
cout << "End of Test" << endl;
return EXIT_SUCCESS;
}

When compiled with Gnu G++ 3.3.1, I get the following
error message (line numbers omitted):
$ g++ -o main.cpp extern_names.cpp
In function `int main()':
error: `Hello_Text' is not a valid template argument
it must be the address of an object with
external linkage
no matching function for call to
`print_text()'
error: `World_Text' is not a valid template argument
it must be the address of an object with
external linkage
no matching function for call to
`print_text()'

I know that the following two statments are different:
(1) extern const char text[] = "Hello";
(2) extern const char * text = "Hello";
because (1) allocates an array of 6 locations and
(2) creates a pointer to a 'C' string literal.

But because the variable 'text' above in (1) is an
array and it has a location, a pointer can be created
and assigned to the location of the first character.
So, I would expect that
extern const char * text;
would be a pointer that refers to the start of the
character array, much the same as this statment:
extern const char text[];

So why are they different when used as template
parameters?

Does Chris Torek's "The Rule" apply here?
http://web.torek.net/torek/c/pa.html

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-31-2004
* Thomas Matthews:
>
> I'm puzzled over the difference between:
> extern const char *
> and
> extern const char [].
> My understanding is that the two are equivalent,
> but the program below shows elsewise.


The first is a pointer, the second is an array.

An array can decay to a pointer e.g. when used as an argument or
in an assignment, and a pointer can be used syntactically like an
array, and vice versa (due to the decay to pointer, an automatic
&a[0] supplied by the compiler, wherever necessary).

But it's not the same thing at all; in particular an array can
and usually will occupy much more storage than a pointer.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-31-2004
"Alf P. Steinbach" <> wrote...
> * Thomas Matthews:
> >
> > I'm puzzled over the difference between:
> > extern const char *
> > and
> > extern const char [].
> > My understanding is that the two are equivalent,
> > but the program below shows elsewise.

>
> The first is a pointer, the second is an array.
>
> An array can decay to a pointer e.g. when used as an argument or
> in an assignment, and a pointer can be used syntactically like an
> array, and vice versa (due to the decay to pointer, an automatic
> &a[0] supplied by the compiler, wherever necessary).
>
> But it's not the same thing at all; in particular an array can
> and usually will occupy much more storage than a pointer.


Which is a bit misleading, Alf. In the case of an extern const char
it is more likely used for some non-changing C-string, in which case
the array will be allocated of the size to store only the characters
in the initialising literal, while with a pointer the compiler will
have to allocate yet additional 4 bytes for the pointer itself. Yes,
the array is larger than a pointer, but one always must look at the
whole picture. The pointer has to point somewhere, and that is some
additional storage. So the fact that an array itself is larger than
a single pointer plays really no role.

Victor


 
Reply With Quote
 
=?ISO-8859-2?Q?Mateusz_=A3oskot?=
Guest
Posts: n/a
 
      07-31-2004
On 7/31/2004 5:46 PM, Alf P. Steinbach wrote:
> But it's not the same thing at all; in particular an array can
> and usually will occupy much more storage than a pointer.


I'd like to add one thing.
All this pointer<->array (first element) "conversion" is not explicit.

Greets
--

Mateusz Łoskot
mateusz at loskot dot net
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-31-2004
* Victor Bazarov:
> "Alf P. Steinbach" <> wrote...
> > * Thomas Matthews:
> > >
> > > I'm puzzled over the difference between:
> > > extern const char *
> > > and
> > > extern const char [].
> > > My understanding is that the two are equivalent,
> > > but the program below shows elsewise.

> >
> > The first is a pointer, the second is an array.
> >
> > An array can decay to a pointer e.g. when used as an argument or
> > in an assignment, and a pointer can be used syntactically like an
> > array, and vice versa (due to the decay to pointer, an automatic
> > &a[0] supplied by the compiler, wherever necessary).
> >
> > But it's not the same thing at all; in particular an array can
> > and usually will occupy much more storage than a pointer.

>
> Which is a bit misleading, Alf. In the case of an extern const char
> it is more likely used for some non-changing C-string, in which case
> the array will be allocated of the size to store only the characters
> in the initialising literal, while with a pointer the compiler will
> have to allocate yet additional 4 bytes for the pointer itself. Yes,
> the array is larger than a pointer, but one always must look at the
> whole picture. The pointer has to point somewhere, and that is some
> additional storage.


Yep, agreed.


> So the fact that an array itself is larger than a single pointer plays
> really no role.


It does in this context; I was talking about interchangability.

They're not interchangable declarations, which is most easily seen from
the fact that they denote objects of usually different memory sizes.

The problem is that with many compilers+linkers using one where the
other should be can compile and link, but gives a non-functioning program.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
tom_usenet
Guest
Posts: n/a
 
      08-02-2004
On Sat, 31 Jul 2004 15:35:48 GMT, Thomas Matthews
<> wrote:

>Hi,
>
>I'm puzzled over the difference between:
> extern const char *
>and
> extern const char [].
>My understanding is that the two are equivalent,
>but the program below shows elsewise.
>
>Given the simple program below:
>// File names.cpp
>extern const char Hello_Text[] = "Hello";
>extern const char World_Text[] = "World!";
>
>// File main.cpp
>#include <iostream>
>#include <cstdlib>
>using std::cout;
>using std::endl;
>
>template <const char * name>
>void print_text(void) // personal pref. here
>{
> cout << name << endl;
> return;
>}
>
>/* Refer to variables in names.cpp */
>/* Note declaration of char * here */
>/* versus char [] in names.cpp. */
>extern const char * Hello_Text;
>extern const char * World_Text;
>
>int main(void)
>{
> cout << "Start of Test" << endl;
> print_text<Hello_Text>();
> print_text<World_Text>();
> cout << "End of Test" << endl;
> return EXIT_SUCCESS;
>}
>
>When compiled with Gnu G++ 3.3.1, I get the following
>error message (line numbers omitted):
>$ g++ -o main.cpp extern_names.cpp
> In function `int main()':
>error: `Hello_Text' is not a valid template argument
> it must be the address of an object with
> external linkage
> no matching function for call to
> `print_text()'
>error: `World_Text' is not a valid template argument
> it must be the address of an object with
> external linkage
> no matching function for call to
> `print_text()'
>
>I know that the following two statments are different:
>(1) extern const char text[] = "Hello";
>(2) extern const char * text = "Hello";
>because (1) allocates an array of 6 locations and
>(2) creates a pointer to a 'C' string literal.
>
>But because the variable 'text' above in (1) is an
>array and it has a location, a pointer can be created
>and assigned to the location of the first character.
>So, I would expect that
> extern const char * text;
>would be a pointer that refers to the start of the
>character array, much the same as this statment:
> extern const char text[];
>
>So why are they different when used as template
>parameters?


You have two different problems here. Firstly, with pointer template
parameters, you must either explicitly or implicitly pass the address
of an object with external linkage. Above, you're trying to pass an
object with external linkage itself rather than its address.

Secondly, even if it did compile you've got a linker error (although
it isn't required to be diagnosed, and it will link successfully on
many platforms) since your const char* is a different type from the
extern variables in the other TU - you need const char[] as the type
(which is an incomplete type). Declarations of arrays are allowed to
differ in the size of the last array bound, but not in whether they
are an array or pointer.

Tom
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference of extern short *x and extern short x[]? Andre C Programming 5 07-17-2012 07:38 PM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
Is char** (or char*[]) implicitly convertible to 'const char * const *'? kevin.hall@motioneng.com C Programming 24 10-30-2005 08:07 AM
Exact difference between 'const char *' and 'char *', also diff between 'const' and 'static' Santa C Programming 1 07-17-2003 02:10 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57