Can someone tell me why C can't support the following 
Author Message
 Can someone tell me why C can't support the following

I know the following is illegal but I would really really like to know
why C can't support the following construct:

typedef struct _mytbl {
  char  *str;          ; Leagal
  int   *data;         ; Illegal (see below)

Quote:
} mytbl[] = {"Not Defined", {0},

             "Square",      {1,2,3,4,0},
             "Circle",      {1,2,0},

Quote:
};

Note1: I want data to point to an array of integer who's length is determined
by a {} list. I could do something silly like "int data[10]" and assume all
lists have 10 or fewer elements.

NOTE2:  I am NOT asking the compiler to generate a variable size structure
called mytbl where data varies in size. I AM asking it to allocate a vector
of integer and assign the address of that integer to data. Wouldn't this be
a nice feature....


I don't read the news group!

--
Alan Krantz

Emory University, Atlanta, GA



Tue, 04 Aug 1998 03:00:00 GMT  
 Can someone tell me why C can't support the following

Quote:

>I know the following is illegal but I would really really like to know
>why C can't support the following construct:

>typedef struct _mytbl {

This begins a type declaration. So far so good...

Quote:
>  char  *str;          ; Leagal
>  int   *data;         ; Illegal (see below)

Nothing illegal about a char * structure member.

Quote:
>} mytbl[] = {"Not Defined", {0},
>             "Square",      {1,2,3,4,0},
>             "Circle",      {1,2,0},

oops! Now you are declaring storage. You can't declare storage inside a type
definition!

Quote:
>};

>Note1: I want data to point to an array of integer who's length is determined
>by a {} list. I could do something silly like "int data[10]" and assume all
>lists have 10 or fewer elements.

>NOTE2:  I am NOT asking the compiler to generate a variable size structure

You sure aren't. You are asking the compiler to generate an error. What you
have is not a C construct.

Quote:
>called mytbl where data varies in size. I AM asking it to allocate a vector
>of integer and assign the address of that integer to data. Wouldn't this be
>a nice feature....

The C language has such a feature. I have _statically_ declared entire _trees_!

The correct way to do it is to remove the typedef. Either that, or do the
typedef like this:

typedef struct _mytbl {
        char *str;
        int *data;

Quote:
} mytbl_type;

int p[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23 }; /* vector of integers */

mytbl_type mytbl[] = {
        { "First Prime", &p[0] },  /* string, address of integer */
        { "Second Prime", &p[1] },
        { "Third Prime", &p[3] },
        /* ... */
        { "Ninth Prime", &p[8] },

Quote:
};

This brings us to the real point of your question. Wouldn't it be nice if we
could do:

        int *x = 3;  ?

In short, no---it wouldn't buy you anything. It is allowed for string literals
because C variables are defined in terms of storage units, and no atomic
variable consists of enough storage cells to hold arbitrary strings. Thus we
have a mechanism to create an unnamed piece of storage and statically bind it
to a pointer. Doing so for integer literals would not be very useful.

Even if it were (and there are arguably many useful things that C _could_
have), it would be difficult to draft into existing standards.

Quote:

>I don't read the news group!

Then don't write to it.
--


Wed, 05 Aug 1998 03:00:00 GMT  
 Can someone tell me why C can't support the following

Quote:

> I know the following is illegal but I would really really like to
> know why C can't support the following construct:

I don't know why standard C doesn't allow this.  Probably just
simplicity: there are lots of possibly worthy additions, and the
committees have only so much time to discuss them all.  For what it's
worth, GNU C supports the following:

struct _mytbl {
  char  *str;
  int   *data;

Quote:
} mytbl[] = {{"Not Defined", (int []){0}},

             {"Square",      (int []){1,2,3,4,0}},
             {"Circle",      (int []){1,2,0}},
Quote:
};

--
Bruce                   Institute of Advanced Scientific Computation

http://supr.scm.liv.ac.uk/~bruce/


Wed, 05 Aug 1998 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Can someone tell me why C can't support the following

2. Determining what canned preprocessor symbols are available

3. Beginner [Q] Using Canned File Open/Save dialog

4. Help with compiling a "canned" program

5. Canned Dialogs

6. Why doesn't the following work?

7. (newbie)why won't the following compile??

8. Tell me why this doesn't work!

9. Can some one tell me why I can't read some newsgroup

10. CS Prof tells it all.

11. CS Prof tells it all...

12. Can someone help me with the following Exercise

 

 
Powered by phpBB® Forum Software