invalid initializer

M

Marty M

The following causes the "invalid initializer" message during gcc
compile time...
char firstword[] = word(question,1);

the "word" function is...
char * word(char *phrase, int what)
{...body here...}

I'm fairly new to C. What is wrong with the above?
PS: All I'm trying to do is to get the first word on a question, so
I'm tryin to roll my own code, should I use something else? Thanks!
--Marty
 
B

Ben Pfaff

The following causes the "invalid initializer" message during gcc
compile time...
char firstword[] = word(question,1);

the "word" function is...
char * word(char *phrase, int what)
{...body here...}

I'm fairly new to C. What is wrong with the above?

If the declaration above is at file scope (declared outside any
function), then the problem is that its initializer is not a
compile-time constant. An expression that depends on the result
of a function call is never a compile-time constant, even if its
return value can be determined at compile time.
 
R

Russell Hanneken

Ben said:
The following causes the "invalid initializer" message during gcc
compile time...
char firstword[] = word(question,1);

the "word" function is...
char * word(char *phrase, int what)
{...body here...}

What is wrong with the above?

If the declaration above is at file scope (declared outside any
function), then the problem is that its initializer is not a
compile-time constant. An expression that depends on the result
of a function call is never a compile-time constant, even if its
return value can be determined at compile time.

Someone please correct me if I'm wrong:

Even if the declaration is not at file scope, it would be illegal in both
C90 and C99. C90 demands compile-time constant initializers for automatic
and register arrays. And both C90 and C99 require a character array to be
initialized with a) a string literal, or b) a brace-enclosed initializer
list. The expression "foo()" doesn't qualify as either.

Is that all correct?
 
B

Ben Pfaff

Russell Hanneken said:
Ben said:
The following causes the "invalid initializer" message during gcc
compile time...
char firstword[] = word(question,1);

the "word" function is...
char * word(char *phrase, int what)
{...body here...}

What is wrong with the above?

If the declaration above is at file scope (declared outside any
function), then the problem is that its initializer is not a
compile-time constant. An expression that depends on the result
of a function call is never a compile-time constant, even if its
return value can be determined at compile time.

Someone please correct me if I'm wrong:

Even if the declaration is not at file scope, it would be illegal in both
C90 and C99. C90 demands compile-time constant initializers for automatic
and register arrays. And both C90 and C99 require a character array to be
initialized with a) a string literal, or b) a brace-enclosed initializer
list. The expression "foo()" doesn't qualify as either.

Oops, you are correct.
 
R

Richard Heathfield

Russell said:
Someone please correct me if I'm wrong:

Even if the declaration is not at file scope, it would be illegal in both
C90 and C99. C90 demands compile-time constant initializers for automatic
and register arrays. And both C90 and C99 require a character array to be
initialized with a) a string literal, or b) a brace-enclosed initializer
list. The expression "foo()" doesn't qualify as either.

Is that all correct?

Um, the expression "foo()" /is/ a string literal.

<g,d&rlh>
 
R

Robert Stankowic

Richard Heathfield said:
Russell Hanneken wrote:

[....]
The expression "foo()" doesn't qualify as either.

Um, the expression "foo()" /is/ a string literal.

<g,d&rlh>
^^^^^^^^^^^^^^^
Um, why?
AFAICS you are perfectly right.

Happy new year to you all.
Robert
 
B

Barry Schwarz

The following causes the "invalid initializer" message during gcc
compile time...
char firstword[] = word(question,1);

the "word" function is...
char * word(char *phrase, int what)
{...body here...}

I'm fairly new to C. What is wrong with the above?
PS: All I'm trying to do is to get the first word on a question, so
I'm tryin to roll my own code, should I use something else? Thanks!
--Marty

Aside from all the other problems, a pointer to char is never an
acceptable initializer for an array of char.

Consider the following, with the same prototype for word():

char *p = word(question,1);
char firstword[] = p;

Unless you initialize it with a string literal or an initializer list,
you cannot expect to define an array or char with no value in the
brackets.


<<Remove the del for email>>
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top