ref. to array != ptr

Blair P. Houghton bhoughto at cmdnfs.intel.com
Sat Nov 10 05:23:33 AEST 1990


In article <14391 at smoke.brl.mil> gwyn at smoke.brl.mil (Doug Gwyn) writes:
>In article <894 at inews.intel.com> bhoughto at cmdnfs.intel.com (Blair P. Houghton) writes:
>>  is there anything more explicit stating that the declaration of an
>>externally-defined pointer to Type can not specify an array of Type
>>with the same identifier defined externally?
>
>Sure.  3.1.2.6:  "All declarations that refer to the same object or
>function shall have compatible type; otherwise, the behavior is undefined."

Well, yes, I should have mentioned this as part of the inference;
I'm wondering if there isn't anything to the effect of "don't do
this" explicitly mentioning array/pointer confusions in multiple
translation units.

The more 3.1.2.6 and the definitions of compatible pointers and
compatible arrays sink in, though, the clearer the inference
becomes.  I was just hoping that, as was done for a number of
other things, there'd be an example of /*RIGHT*/ and /*WRONG*/
coding.

				--Blair
				  "cf. any of Henry's and
				   my stuff, respectively..."



A long example of the behavior is included below (silly me,
but I did this with Ultrix' cc, which isn't even a
conforming compiler).  I suggest you hit 'n' unless you've
never seen this situation before (although the difference
between the results of defining an array and declaring a
pointer and vice-versa are interesting, even if they
are undefined in conforming implementations).



3 Files (ex1.c, ex2.c, Makefile):

::::::::::::::
/eng/eng21/bhoughto/bin/experiment/ex1.c
::::::::::::::
/* ex1.c -- yahwp; used with ex2.c to show how not to declare external arrays */

/* choose a definition: */		/* proper corresponding declaration: */
#if DEF_AS_PTR
char *hello = "Hello, world!\n";	/* use DECL_AS_PTR in ex2.c */
#else
char hello[] = "Hello, world!\n";	/* use with not DECL_AS_PTR in ex2.c */
#endif

main()
{
    printf("%s\n",hello);
    foo();
}

::::::::::::::
/eng/eng21/bhoughto/bin/experiment/ex2.c
::::::::::::::
/* ex2.c -- the right and wrong way to declare an externally defined array */

/* choose a declaration: */		/* proper corresponding definition: */
#if DECL_AS_PTR
char *hello;				/* use iff DEF_AS_PTR in ex1.c */
#else
char hello[];				/* use iff not DEF_AS_PTR in ex1.c */
#endif

foo()
{
    printf("%s\n",hello);
}

::::::::::::::
/eng/eng21/bhoughto/bin/experiment/Makefile
::::::::::::::
#  testing external array/pointer declaration/confusion

# executable names encode value of (DEF_AS_PTR, DECL_AS_PTR)
external:  ex0 ex1 ex2 ex3

ex0:
	cc -DDEF_AS_PTR=0 -DDECL_AS_PTR=0 -c ex1.c
	cc -DDEF_AS_PTR=0 -DDECL_AS_PTR=0 -c ex2.c
	cc -DDEF_AS_PTR=0 -DDECL_AS_PTR=0 -o ex0 ex1.o ex2.o

ex1:
	cc -DDEF_AS_PTR=0 -DDECL_AS_PTR=1 -c ex1.c
	cc -DDEF_AS_PTR=0 -DDECL_AS_PTR=1 -c ex2.c
	cc -DDEF_AS_PTR=0 -DDECL_AS_PTR=1 -o ex1 ex1.o ex2.o

ex2:
	cc -DDEF_AS_PTR=1 -DDECL_AS_PTR=0 -c ex1.c
	cc -DDEF_AS_PTR=1 -DDECL_AS_PTR=0 -c ex2.c
	cc -DDEF_AS_PTR=1 -DDECL_AS_PTR=0 -o ex2 ex1.o ex2.o

ex3:
	cc -DDEF_AS_PTR=1 -DDECL_AS_PTR=1 -c ex1.c
	cc -DDEF_AS_PTR=1 -DDECL_AS_PTR=1 -c ex2.c
	cc -DDEF_AS_PTR=1 -DDECL_AS_PTR=1 -o ex3 ex1.o ex2.o



Results (name of executable encodes setting of (DEF_AS_PTR,DECL_AS_PTR):

Script started on Fri Nov  9 11:09:33 1990
prompt> ex0
Hello, world!

Hello, world!

prompt> ex1
Hello, world!

Segmentation fault (core dumped)
prompt> ex2
Hello, world!

8^P
prompt> ex3
Hello, world!

Hello, world!

prompt> ^D
script done on Fri Nov  9 11:10:03 1990



More information about the Comp.std.c mailing list