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

From: Thomas Matthews (Thomas_MatthewsSpamBotsSuck_at_sbcglobal.net)
Date: 07/31/04


Date: Sat, 31 Jul 2004 15:35:48 GMT

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


Relevant Pages

  • Re: Compiling difficulties
    ... > template ... reference to a pointer to const char. ...
    (microsoft.public.vc.language)
  • Re: My normal template keeps changing.
    ... If you are moving your customizations to a separate template (not ... That is a separate template in your Word Startup folder. ... See also the MVP FAQ: http://word.mvps.org/FAQs/ which is awesome! ... This message is posted to a newsgroup. ...
    (microsoft.public.word.application.errors)
  • Re: Use Word template from Outlook
    ... See also the MVP FAQ: http://word.mvps.org/FAQs/ which is awesome! ... This message is posted to a newsgroup. ... the macrobutton field is meant to ... new document in word based upon the template & double click the ...
    (microsoft.public.word.docmanagement)
  • Re: Use Word template from Outlook
    ... already knows (i.e. template, name address, date, auto prompts), and for me ... See also the MVP FAQ: http://word.mvps.org/FAQs/ which is awesome! ... This message is posted to a newsgroup. ... the macrobutton field is meant to ...
    (microsoft.public.word.docmanagement)
  • Re: Item 42 of Effective C++
    ... > variable in the class are of the template type? ... several times does not lead to code bloat, and that is probably also not ... void pointer. ... believe at least knowing a bit of assembler helps a lot for understanding ...
    (alt.comp.lang.learn.c-cpp)