linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* 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
* 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

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).