Initializer element is not constant
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
A message that is not in the archive (sent off-list or not yet received): [email protected]
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
Quoted 56 lines
Quoted 51 lines · Scott
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_tlen; /* 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 forrecip_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 [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 7 lines · Ron Michael Khu
Hi,
can u point us to line 124??
Quoted 3 lines
Quoted 3 lines
DATUM recip_data[] = {
{ namectrl,NAMECTRL_LEN,make_upper}, <-- Line 124
{ name1,NAME1_LEN,make_upper},
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....
Quoted 22 lines · Scott
Quoted 11 lines · Ron Michael Khu
Hi,
can u point us to line 124??
Quoted 5 lines
Quoted 5 lines
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 [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 8 lines · Ron Michael Khu
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....
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. =)
Quoted 42 lines · Scott
Quoted 9 lines · Ron Michael Khu
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 [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 9 lines · Ron Michael Khu
but u can.... =)
have u tried dynamically allocating space forrecip_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....
Hi Scott,
Quoted 1 line · Scott
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
Signature · 4 lines
``There's no dark side of the moon, really Matter of fact, it's all dark'' --Pink Floyd
Quoted 14 lines · Christoph Bussenius
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_tlen; /* max len of the data */
char *(*xlat)(); /* translation routine */
} DATUM;
Quoted 6 lines
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