By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,246 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

invalid initializer

P: n/a
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
Nov 14 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
ma***********@comcast.net (Marty M) writes:
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.
--
"We put [the best] Assembler programmers in a little glass case in the hallway
near the Exit sign. The sign on the case says, `In case of optimization
problem, break glass.' Meanwhile, the problem solvers are busy doing their
work in languages most appropriate to the job at hand." --Richard Riehle
Nov 14 '05 #2

P: n/a
Ben Pfaff wrote:
ma***********@comcast.net (Marty M) writes:
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?

--
Russell Hanneken
rg********@pobox.com
Remove the 'g' from my address to send me mail.
Nov 14 '05 #3

P: n/a
"Russell Hanneken" <rg********@pobox.com> writes:
Ben Pfaff wrote:
ma***********@comcast.net (Marty M) writes:
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.
--
"In My Egotistical Opinion, most people's C programs should be indented six
feet downward and covered with dirt." -- Blair P. Houghton
Nov 14 '05 #4

P: n/a
Russell Hanneken wrote:
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>

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #5

P: n/a

"Richard Heathfield" <do******@address.co.uk.invalid> schrieb im Newsbeitrag
news:bs**********@sparta.btinternet.com...
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

Nov 14 '05 #6

P: n/a
On 30 Dec 2003 21:18:45 -0800, ma***********@comcast.net (Marty M)
wrote:
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>>
Nov 14 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.