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

From: tom_usenet (tom_usenet_at_hotmail.com)
Date: 08/02/04


Date: Mon, 02 Aug 2004 11:36:33 +0100

On Sat, 31 Jul 2004 15:35:48 GMT, Thomas Matthews
<Thomas_MatthewsSpamBotsSuck@sbcglobal.net> 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



Relevant Pages

  • Re: Why is "Hello World" const char* ?
    ... > Your statement is incorrect. ... Since the first character is of type (const char) as ... > pointer to const char. ... The type of a string literal is "array of char", ...
    (comp.lang.c)
  • Re: Why is "Hello World" const char* ?
    ... > Your statement is incorrect. ... Since the first character is of type (const char) as ... > pointer to const char. ... The type of a string literal is "array of char", ...
    (comp.lang.cpp)
  • Re: declaration error
    ... 'message1' is the name of an array of const char. ... msgptr12has type 'pointer to array of const char'. ...
    (comp.lang.c)
  • Re: string literals question
    ... The type of a string literal in C is array of char, ... When converted to a pointer, ... not pointer to const char. ... Attempting to modify a string literal in C is undefined behavior not ...
    (comp.lang.c)
  • Re: why still use C?
    ... Using extern "C" is not sufficient to cover those cases. ... A function or function pointer ... library required them to send the compiler output through a "warning ... >> times the size of the thing we really want, cast the resultant pointer to ...
    (comp.lang.c)