473,554 Members | 3,010 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 30234
ma***********@c omcast.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***********@c omcast.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********@pobo x.com
Remove the 'g' from my address to send me mail.
Nov 14 '05 #3
"Russell Hanneken" <rg********@pob ox.com> writes:
Ben Pfaff wrote:
ma***********@c omcast.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.pow ernet.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******@addre ss.co.uk.invali d> schrieb im Newsbeitrag
news:bs******** **@sparta.btint ernet.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***********@c omcast.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
2989
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 says). But in the example below the copy constructor of Vec is executed when initializing the Ref object. Is this standard conform? Doesn't it...
2
19546
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
2236
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 an attempt to write to this pointer causes the program to exit with a core dump. Output after compiling with HP c-compiler: 1. ffffff78 1. 7eff3358
9
2359
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 class, if the class has more than than one cosntructor, by making sure that each constructor has a different signature. I have managed to learn that...
4
6195
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 myintArray = { -1, -1 }; static int arr_argc = 0;
3
2128
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
3545
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 uncaught exception. How to catch an exception that happened before main() try { ... } catch (...) { ... } block? Is there a way?
5
8179
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
2622
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 issue occurred in the first place. The project uses a simple template class that acts as a buffer. Initially it has a fixed length, but it's append...
0
7584
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7512
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8022
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7546
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
3546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3535
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1117
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
827
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.