linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Initializer element is not constant
@ 2005-01-20 23:21 Scott
       [not found] ` <41F06AC1.2000605@hq.ntsp.nec.co.jp>
  0 siblings, 1 reply; 11+ messages in thread
From: Scott @ 2005-01-20 23:21 UTC (permalink / raw)
  To: linux-c-programming

Hi,

It has been a LONG TIME since I've done any programming in C. I have
two old programs still in use which were developed on msdos, using the
"Mark Williams Let's C" compiler. We're talking 1980's stuff here.

Anyway, I thought to try to port them to linux. Can anyone tell me
what I'm doing wrong?

Here are the relevant portions of indatax.c:

char *namectrl = NULL;
char *name1 = NULL;
char *name2 = NULL;

typedef struct {
	char *var;	/* destination for storage of the data */
	size_t len;	/* max len of the data */
	char *(*xlat)();	/* translation routine */
} DATUM;

DATUM recip_data[] = {
{  namectrl, NAMECTRL_LEN, make_upper },
{  name1, NAME1_LEN, make_upper },
{  name2, NAME2_LEN, make_upper },
[...]

And these are the errors...

indatax.c:124: initializer element is not constant
indatax.c:124: (near initialization for `recip_data[0].var')
indatax.c:125: initializer element is not constant
indatax.c:125: (near initialization for `recip_data[1].var')
indatax.c:126: initializer element is not constant
indatax.c:126: (near initialization for `recip_data[2].var')
[...]

gcc version 2.95.3

Thanks,

Scott Swanson
Pendroy, Montana

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Initializer element is not constant
       [not found] ` <41F06AC1.2000605@hq.ntsp.nec.co.jp>
@ 2005-01-21  2:37   ` Ron Michael Khu
  2005-01-21 16:27     ` Scott
  0 siblings, 1 reply; 11+ messages in thread
From: Ron Michael Khu @ 2005-01-21  2:37 UTC (permalink / raw)
  To: linux-c-programming; +Cc: Scott

Hi,

can u point us to line 124??

I've encountered a similar error message before when i using HP-UX on 
Intel Itanium.
The compiler complained that i used a non-constant in a static array 
declaration...
char a[ maxLen ] ;

in the end, I had to malloc/calloc them... (since i didnt know how to 
tweak or instruct
the compiler to ignore such errors... since the code was already old and 
compilable on
other hp-ux platforms)

-Ron

>
> Scott wrote:
>
>> Hi,
>>
>> It has been a LONG TIME since I've done any programming in C. I have
>> two old programs still in use which were developed on msdos, using the
>> "Mark Williams Let's C" compiler. We're talking 1980's stuff here.
>>
>> Anyway, I thought to try to port them to linux. Can anyone tell me
>> what I'm doing wrong?
>>
>> Here are the relevant portions of indatax.c:
>>
>> char *namectrl = NULL;
>> char *name1 = NULL;
>> char *name2 = NULL;
>>
>> typedef struct {
>>     char *var;    /* destination for storage of the data */
>>     size_t len;    /* max len of the data */
>>     char *(*xlat)();    /* translation routine */
>> } DATUM;
>>
>> DATUM recip_data[] = {
>> {  namectrl, NAMECTRL_LEN, make_upper },
>> {  name1, NAME1_LEN, make_upper },
>> {  name2, NAME2_LEN, make_upper },
>> [...]
>>
>> And these are the errors...
>>
>> indatax.c:124: initializer element is not constant
>> indatax.c:124: (near initialization for `recip_data[0].var')
>> indatax.c:125: initializer element is not constant
>> indatax.c:125: (near initialization for `recip_data[1].var')
>> indatax.c:126: initializer element is not constant
>> indatax.c:126: (near initialization for `recip_data[2].var')
>> [...]
>>
>> gcc version 2.95.3
>>
>> Thanks,
>>
>> Scott Swanson
>> Pendroy, Montana
>> -
>> To unsubscribe from this list: send the line "unsubscribe 
>> linux-c-programming" in
>> the body of a message to majordomo@vger.kernel.org
>> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>>
>>
>>  
>>
>
>



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Initializer element is not constant
  2005-01-21  2:37   ` Ron Michael Khu
@ 2005-01-21 16:27     ` Scott
  2005-01-21 17:46       ` Ron Michael Khu
  0 siblings, 1 reply; 11+ messages in thread
From: Scott @ 2005-01-21 16:27 UTC (permalink / raw)
  To: linux-c-programming

On Fri, Jan 21, 2005 at 10:37:12AM +0800, Ron Michael Khu wrote:
> Hi,
> 
> can u point us to line 124??
> 
> >>DATUM recip_data[] = {
> >>{  namectrl, NAMECTRL_LEN, make_upper },        <-- Line 124
> >>{  name1, NAME1_LEN, make_upper },

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Initializer element is not constant
  2005-01-21 16:27     ` Scott
@ 2005-01-21 17:46       ` Ron Michael Khu
  2005-01-21 18:54         ` Scott
  0 siblings, 1 reply; 11+ messages in thread
From: Ron Michael Khu @ 2005-01-21 17:46 UTC (permalink / raw)
  To: Scott; +Cc: linux-c-programming

have u tried changing the first field of each element??
something like... replacing those char ptrs with something static?

example:

DATUM recip_data[] = {
{  "i am a constant str", NAMECTRL_LEN, make_upper },       
{  "i am a constant str", NAME1_LEN, make_upper },

u might notice that some of  "initializer element is not constant" 
errors will be significantly reduced....

perhaps, the compiler is complaining that u have used something 
"dynamic"(or at least non-static) in defining a
"statically" allocated data structure(recip_data)
=(

perhaps u can change the way u define/declare ur variables/data 
structures....


Scott wrote:

>On Fri, Jan 21, 2005 at 10:37:12AM +0800, Ron Michael Khu wrote:
>  
>
>>Hi,
>>
>>can u point us to line 124??
>>
>>    
>>
>>>>DATUM recip_data[] = {
>>>>{  namectrl, NAMECTRL_LEN, make_upper },        <-- Line 124
>>>>{  name1, NAME1_LEN, make_upper },
>>>>        
>>>>
>-
>To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
>the body of a message to majordomo@vger.kernel.org
>More majordomo info at  http://vger.kernel.org/majordomo-info.html
>
>
>  
>




^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Initializer element is not constant
  2005-01-21 17:46       ` Ron Michael Khu
@ 2005-01-21 18:54         ` Scott
  2005-01-21 19:50           ` Ron Michael Khu
  0 siblings, 1 reply; 11+ messages in thread
From: Scott @ 2005-01-21 18:54 UTC (permalink / raw)
  To: linux-c-programming

On Sat, Jan 22, 2005 at 01:46:31AM +0800, Ron Michael Khu wrote:
> have u tried changing the first field of each element??
> something like... replacing those char ptrs with something static?
> 
> example:
> 
> DATUM recip_data[] = {
> {  "i am a constant str", NAMECTRL_LEN, make_upper },       
> {  "i am a constant str", NAME1_LEN, make_upper },

As I recalled, the ability to HAVE dynamic pointers was one of the
primary benefits of using c in the first place! Later in the file I
have this routine which allocates space for each pointer in the
structure:

alloc_space(r) RECORD *r;
{	register size_t i;
	register char **v;
	
	for(i=0; i<r->n; i++)
	{	if(r->data[i].len == 0 || r->data[i].len == 0xFFFF) continue;
		v = (r->data[i].var);
		if (*v == NULL)
		{	*v = (char *) calloc(1,r->data[i].len + 1);
			memset(*v,'A'+i,r->data[i].len);
		}
	}
}

I guess i just don't understand why a "modern" compiler wants to
restrict my options in this fashion....

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Initializer element is not constant
  2005-01-21 18:54         ` Scott
@ 2005-01-21 19:50           ` Ron Michael Khu
  2005-01-21 21:31             ` Scott
  0 siblings, 1 reply; 11+ messages in thread
From: Ron Michael Khu @ 2005-01-21 19:50 UTC (permalink / raw)
  To: Scott, linux-c-programming

but u can.... =)
have u tried dynamically allocating space for recip_data?? instead of 
the usual static declaration?
char a[10] <-- this is static
char a[]= {'1','2','3','4'} <-- this is static with definition and 
declartion in one
char *a = calloc.....

have u tried it? using malloc/calloc for defining DATUM recip_data??

im not good with explanations....
but i bet the gurus like JBG could explain the solution/scenario alot 
better. =)


Scott wrote:

>On Sat, Jan 22, 2005 at 01:46:31AM +0800, Ron Michael Khu wrote:
>  
>
>>have u tried changing the first field of each element??
>>something like... replacing those char ptrs with something static?
>>
>>example:
>>
>>DATUM recip_data[] = {
>>{  "i am a constant str", NAMECTRL_LEN, make_upper },       
>>{  "i am a constant str", NAME1_LEN, make_upper },
>>    
>>
>
>As I recalled, the ability to HAVE dynamic pointers was one of the
>primary benefits of using c in the first place! Later in the file I
>have this routine which allocates space for each pointer in the
>structure:
>
>alloc_space(r) RECORD *r;
>{	register size_t i;
>	register char **v;
>	
>	for(i=0; i<r->n; i++)
>	{	if(r->data[i].len == 0 || r->data[i].len == 0xFFFF) continue;
>		v = (r->data[i].var);
>		if (*v == NULL)
>		{	*v = (char *) calloc(1,r->data[i].len + 1);
>			memset(*v,'A'+i,r->data[i].len);
>		}
>	}
>}
>
>I guess i just don't understand why a "modern" compiler wants to
>restrict my options in this fashion....
>-
>To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
>the body of a message to majordomo@vger.kernel.org
>More majordomo info at  http://vger.kernel.org/majordomo-info.html
>
>
>  
>



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Initializer element is not constant
  2005-01-21 19:50           ` Ron Michael Khu
@ 2005-01-21 21:31             ` Scott
  2005-01-22 21:12               ` Christoph Bussenius
  0 siblings, 1 reply; 11+ messages in thread
From: Scott @ 2005-01-21 21:31 UTC (permalink / raw)
  To: linux-c-programming

On Sat, Jan 22, 2005 at 03:50:02AM +0800, Ron Michael Khu wrote:
> but u can.... =)
> have u tried dynamically allocating space for recip_data?? instead of 
> the usual static declaration?
> char a[10] <-- this is static
> char a[]= {'1','2','3','4'} <-- this is static with definition and 
> declartion in one
> char *a = calloc.....
> 
> have u tried it? using malloc/calloc for defining DATUM recip_data??

Well yes, I guess I could [m|c]alloc the recip_data array. But how do I
then populate it?

 recip_data[0].var = namectrl;
 recip_data[0].len = NAMECTRL_LEN;
 recip_data[0].xlat = make_upper;
etc...

How tedious is THAT!? Especially considering that the code has
eight such arrays, averaging 6 elements per array.

Maybe I'm not understanding your suggestion. As I mentioned, it has
been quite a while since I did any c programming. I just find it
strange that code which compiled fine 20 years ago now will not. I
could understand if it just generated warnings....



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Initializer element is not constant
  2005-01-21 21:31             ` Scott
@ 2005-01-22 21:12               ` Christoph Bussenius
  2005-01-24 20:44                 ` Scott
  0 siblings, 1 reply; 11+ messages in thread
From: Christoph Bussenius @ 2005-01-22 21:12 UTC (permalink / raw)
  To: linux-c-programming

Hi Scott,

On Fri, Jan 21, 2005 at 02:31:57PM -0700, Scott wrote:
>  recip_data[0].var = namectrl;

namectrl is NULL, so why would you want to do this?  This is just like
writing

recip_data[0] = NULL;

I am not quite sure what you intend to do, but I guess that you want
namectrl to change when you change recip_data[0].  To accomplish this,
you could make recip_data[0] a pointer to a pointer:

char *namectrl = NULL;
char *name1 = NULL;
char *name2 = NULL;

typedef struct {
        char **var;      /* destination for storage of the data */
        size_t len;     /* max len of the data */
        char *(*xlat)();        /* translation routine */
} DATUM;                                                                                        

DATUM recip_data[] = {
{  &namectrl, NAMECTRL_LEN, make_upper },
{  &name1, NAME1_LEN, make_upper },
{  &name2, NAME2_LEN, make_upper },
[...]

Now you can change namectrl by saying
*recip_data[0].var = "test string";

Hope this helps,
Christoph

-- 
``There's no dark side of the moon, really
Matter of fact, it's all dark''

--Pink Floyd

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Initializer element is not constant
  2005-01-22 21:12               ` Christoph Bussenius
@ 2005-01-24 20:44                 ` Scott
  0 siblings, 0 replies; 11+ messages in thread
From: Scott @ 2005-01-24 20:44 UTC (permalink / raw)
  To: linux-c-programming

On Sat, Jan 22, 2005 at 10:12:06PM +0100, Christoph Bussenius wrote:
> I am not quite sure what you intend to do, but I guess that you want
> namectrl to change when you change recip_data[0].  To accomplish this,
> you could make recip_data[0] a pointer to a pointer:
> 
> char *namectrl = NULL;
> char *name1 = NULL;
> char *name2 = NULL;
> 
> typedef struct {
>         char **var;      /* destination for storage of the data */
>         size_t len;     /* max len of the data */
>         char *(*xlat)();        /* translation routine */
> } DATUM;                                                                                        
> 
> DATUM recip_data[] = {
> {  &namectrl, NAMECTRL_LEN, make_upper },
> {  &name1, NAME1_LEN, make_upper },
> {  &name2, NAME2_LEN, make_upper },
> [...]
> 

Right! As I said, my c is quite rusty. Now as I recall, there was some
problem with the old dos compiler which prevented this syntax but
allowed the erroneous one. That, or I didn't understand what I was
doing and the compiler let me get away with it.... Looking at the code
in more detail, I see that it relies upon some bcd math module I'd written
in assembler, so the odds of this getting ported are pretty
slim. Thanks for the help, though!

Scott Swanson


^ permalink raw reply	[flat|nested] 11+ messages in thread

* RE: Initializer element is not constant
@ 2005-01-24 21:09 Huber, George K RDECOM CERDEC STCD SRI
  2005-01-24 21:28 ` Scott
  0 siblings, 1 reply; 11+ messages in thread
From: Huber, George K RDECOM CERDEC STCD SRI @ 2005-01-24 21:09 UTC (permalink / raw)
  To: 'Scott', linux-c-programming

Scott wrote:

>Here are the relevant portions of indatax.c:

>char *namectrl = NULL;
>char *name1 = NULL;
>char *name2 = NULL;

>typedef struct {
>	char *var;	/* destination for storage of the data */
>	size_t len;	/* max len of the data */
>	char *(*xlat)();	/* translation routine */
>} DATUM;

>DATUM recip_data[] = {
>{  namectrl, NAMECTRL_LEN, make_upper },
>{  name1, NAME1_LEN, make_upper },
>{  name2, NAME2_LEN, make_upper },
>[...]

>And these are the errors...

>indatax.c:124: initializer element is not constant
>indatax.c:124: (near initialization for `recip_data[0].var')
>indatax.c:125: initializer element is not constant
>indatax.c:125: (near initialization for `recip_data[1].var')
>indatax.c:126: initializer element is not constant
>indatax.c:126: (near initialization for `recip_data[2].var')
>[...]

First, I would upgrade to a more modern version of gcc.  The 
current release is the 3.4 series.   

Secondly, I tried the following program,

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include <stdio.h>
#include <stdlib.h>

#define NAMECTL_LEN   15
#define NAME_LEN       10

typedef struct tag_datum
{
    char*   var;
    size_t  len;
    char    *(*xlat)();
} datum;

char* make_upper()
{
    return NULL;
}

int main(int argc, char** argv)
{
    char*    namectrl = NULL;
    char*    name1 = NULL;
    char*    name2 = NULL;

    datum    data[] = {{namectrl, NAMECTL_LEN, make_upper},
                       {name1, NAME_LEN, make_upper},
                       {name2, NAME_LEN, make_upper}};

    return 0;
}

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

When I compiled this program using gcc 3.4.2 with the flags `-ansi
-pedantic -Wall' I get a number of warnings stating:
"Initializer element is not computable at load time" 
Two per each line of initializing data.  My guess is that the compiler
can-not figure out how to initialize the first element of the structure
at run time because namectrl has yet to "created".  


However, all is not lost, changing the program to:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include <stdio.h>
#include <stdlib.h>

#define NAMECTL_LEN   15
#define NAME_LEN       10

typedef struct tag_datum
{
    char*   var;
    size_t  len;
    char    *(*xlat)();
} datum;

char* make_upper()
{
    return NULL;
}

int main(int argc, char** argv)
{
    datum    data[] = {{NULL, NAMECTL_LEN, make_upper},
                       {NULL, NAME_LEN, make_upper},
                       {NULL, NAME_LEN, make_upper}};

    return 0;
}

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

results in a clean compilation (except for a warning about
an unused variable).  To initialize the var member of datum, you
should be able to do something like:

  if(NULL != (data[0].var = malloc(NAMECTL_LEN*sizeof(char))))
  {
      strncpy(data[0].var, "test string", NAMECTL_LEN);
  }

and you should be able to use datum like this:

  int   i, num = sizeof(data)/sizeof(datum);
  for(i = 0; i < num; i++)
  {
      printf("data point %d, var %s\n", i, (data[i].var ? data[i].var : "N/A"));
  }

hope this helps,
George

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Initializer element is not constant
  2005-01-24 21:09 Initializer element is not constant Huber, George K RDECOM CERDEC STCD SRI
@ 2005-01-24 21:28 ` Scott
  0 siblings, 0 replies; 11+ messages in thread
From: Scott @ 2005-01-24 21:28 UTC (permalink / raw)
  To: linux-c-programming

On Mon, Jan 24, 2005 at 04:09:30PM -0500, Huber, George K RDECOM CERDEC STCD SRI wrote:
> 
> First, I would upgrade to a more modern version of gcc.  The 
> current release is the 3.4 series.   

Yeah, everything I have on this machine is pretty ancient....

> 
> Secondly, I tried the following program,

   [big snip...]
> 
> hope this helps,
> George

Yes, it certainly does! Thanks so much to you and the others on the
list. I'm sort of excited thinking about coming back to C after a long
sojourn with perl. Can you recommend a good on-line tutorial to
refresh my failing memory?

Scott Swanson

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2005-01-24 21:28 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-01-24 21:09 Initializer element is not constant Huber, George K RDECOM CERDEC STCD SRI
2005-01-24 21:28 ` Scott
  -- strict thread matches above, loose matches on Subject: below --
2005-01-20 23:21 Scott
     [not found] ` <41F06AC1.2000605@hq.ntsp.nec.co.jp>
2005-01-21  2:37   ` Ron Michael Khu
2005-01-21 16:27     ` Scott
2005-01-21 17:46       ` Ron Michael Khu
2005-01-21 18:54         ` Scott
2005-01-21 19:50           ` Ron Michael Khu
2005-01-21 21:31             ` Scott
2005-01-22 21:12               ` Christoph Bussenius
2005-01-24 20:44                 ` Scott

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).