Why are character arrays special (equal rights for structs)

Robert Forsman thoth at beach.cis.ufl.edu
Tue Feb 7 01:10:09 AEST 1989


 I've got a question about what is in the standard concerning
initializing pointers to objects other than chars.  I have a
program that works off of strings of ints (I ran out of char
values and switched up).  Anyway, I occasionally need to do
stuff analogous to

	if (strcmp("hello",s)==0)

but it would probably look like this

	if (intcmp((int*){43,21,5,0},command)==0)

 Now you don't really want to have to waste source code space
to declare int strings, you want to declare them on the fly
just like "hello".  Unfortunately, I can't figure out how to
do this so I have resorted to constructs like this :

	static int *GET_TORCH = { 34,52,0};
	if (intcmp(GET_TORCH,command)==0)

  The problem is that even this gives errors in GCC.
<<initializer for scalar variable requires one element.>>
  It IS one element, a pointer to several elements is one
element!?  When you say char *mesg="enter sum>"; the
compiler doesn't complain.  It figures out that enter sum is
a character string that is supposed to reside in the data
space and assigns mesg the pointer to it.  I finally had to
declare it this way :
	static int GET_TORCH[] = { 34,52,0};
 This is probably the second best way of doing it, the stack
isn't cluttered by pointers that have to be initialized and
the value is static, but the best way would be the first
(int *){ 34,52,0 }, no names to clutter the compiler and I
wouldn't have wasted a line for a declaration.
  The question is, what does the standard say about
structure and array constants (and arrays of structure
constants, etc.)?  I think that to not have the capability
to declare something like that on the fly is crippled.  If
it isn't in the standard, somebody get it in there.  If it
IS in the standard, somebody get GNU to add it to their
compiler (or tell me how it's done).


-----------------------------------------------------------
If you're thinking of sueing my employer over my opinions
then I have no other recourse than to tell you that you are
lower than George Bush and Michael Dukkakis put together.



More information about the Comp.lang.c mailing list