initializer element

M

Mark

gcc-4.2.3 isn't happy with the following snippet:

void foo(char *p)
{
/* warning: initializer element is not computable at load time. */
char *my_argv[] = { "abc", p, "def", NULL };
...
}

int main(void)
{
foo("xyz");
return 0;
}

What am I doing wrong?
 
I

Ian Collins

gcc-4.2.3 isn't happy with the following snippet:

void foo(char *p)
{
/* warning: initializer element is not computable at load time. */
char *my_argv[] = { "abc", p, "def", NULL };
...
}

int main(void)
{
foo("xyz");
return 0;
}

What am I doing wrong?

Not using C99 mode?
 
V

Vincenzo Mercuri

Mark ha scritto:
gcc-4.2.3 isn't happy with the following snippet:

void foo(char *p)
{
/* warning: initializer element is not computable at load time. */
char *my_argv[] = { "abc", p, "def", NULL };
...
}

int main(void)
{
foo("xyz");
return 0;
}

What am I doing wrong?

Also #include <stdio.h> at least, so NULL won't stay undeclared
 
M

Mark

Ian said:
void foo(char *p)
{
/* warning: initializer element is not computable at load time. */
char *my_argv[] = { "abc", p, "def", NULL };
...

[skip]

Not using C99 mode?
Yes, indeed. Such kind of initialization of array elements appeared in C99 ?
 
V

Vincenzo Mercuri

Mark ha scritto:
Ian said:
void foo(char *p)
{
/* warning: initializer element is not computable at load time. */
char *my_argv[] = { "abc", p, "def", NULL };
...

[skip]

Not using C99 mode?
Yes, indeed. Such kind of initialization of array elements appeared in
C99 ?

Yes, only C99 allows you to initialize an array element
with a nonconstant expression (in this case: the pointer p).
 
K

Keith Thompson

Vincenzo Mercuri said:
Mark ha scritto:
Ian said:
void foo(char *p)
{
/* warning: initializer element is not computable at load time. */
char *my_argv[] = { "abc", p, "def", NULL };
...

[skip]

Not using C99 mode?
Yes, indeed. Such kind of initialization of array elements appeared in
C99 ?

Yes, only C99 allows you to initialize an array element
with a nonconstant expression (in this case: the pointer p).

And only if the object being initialized has automatic storage duration
(which it does in this case).
 
K

Keith Thompson

Vincenzo Mercuri said:
Mark ha scritto:
gcc-4.2.3 isn't happy with the following snippet:

void foo(char *p)
{
/* warning: initializer element is not computable at load time. */
char *my_argv[] = { "abc", p, "def", NULL };
...
}

int main(void)
{
foo("xyz");
return 0;
}

What am I doing wrong?

Also #include <stdio.h> at least, so NULL won't stay undeclared

Or #include <stddef.h> if you're not doing any I/O. NULL is defined in
several different standard headers.
 
V

Vincenzo Mercuri

Keith Thompson ha scritto:
Vincenzo Mercuri said:
Mark ha scritto:
gcc-4.2.3 isn't happy with the following snippet:

void foo(char *p)
{
/* warning: initializer element is not computable at load time. */
char *my_argv[] = { "abc", p, "def", NULL };
...
}

int main(void)
{
foo("xyz");
return 0;
}

What am I doing wrong?

Also #include<stdio.h> at least, so NULL won't stay undeclared

Or #include<stddef.h> if you're not doing any I/O. NULL is defined in
several different standard headers.

Yes, with 'at least' I meant that 'stdio.h' would be one
of the ways to have NULL declared. But actually 'stddef.h'
would be more suitable to that example.
 
V

Vincenzo Mercuri

Keith Thompson ha scritto:
Vincenzo Mercuri said:
Mark ha scritto:
Ian Collins wrote:
void foo(char *p)
{
/* warning: initializer element is not computable at load time. */
char *my_argv[] = { "abc", p, "def", NULL };
...


[skip]

Not using C99 mode?
Yes, indeed. Such kind of initialization of array elements appeared in
C99 ?

Yes, only C99 allows you to initialize an array element
with a nonconstant expression (in this case: the pointer p).

And only if the object being initialized has automatic storage duration
(which it does in this case).

Exactly!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top