473,320 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

invalid initializer

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
6 30139
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
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
"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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Alexander Stippler | last post by:
Hi, I wonder about the behaviour of como and icc on some very simple program. I thought initializing members of classes, which are of class type, would be 'direct initialized' (as the standard...
2
by: Todd Nathan | last post by:
Hi. have this code and compiler problem. GCC 2.95.3, BeOS, error "initializer element is not constant" #ifdef FILEIO { static struct { char *sfn; FILE *sfd; } stdfiles = {
5
by: FKothe | last post by:
Hello together, the program below shows a behavior i do not understand. When compiled with the HX-UX11 c-comiler ( version B.11.11.04 ) v2.p in function test_it0 points to an invalid adress and...
9
by: Player | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all. I am in the process of teaching myself C# and I think I am doing OK. I have learnt how to how to call the right constructor of a...
4
by: bob | last post by:
I have little C experience and am concurrently trying to tackle C and LKM's (a little too ambitious maybe) anyway here is the problem I'm having with an example module I found. static int...
3
by: Ham Pastrami | last post by:
class Point { public: const int x, y; Point(int x, int y); } Point::Point(int x, int y) : x(x), y(y) { }
6
by: Marvin Barley | last post by:
I have a class that throws exceptions in new initializer, and a static array of objects of this type. When something is wrong in initialization, CGI program crashes miserably. Debugging shows...
5
by: Pallav singh | last post by:
How can we justify that initializer list is better in performance than assignment list in constructor of C++ ??
68
by: DaveJ | last post by:
Recently I was working on a project where I came across an issue where the program cored and reported "free(): invalid pointer". I found a resolution for this, but don't fully understand why the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.