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 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
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.
"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
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
"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
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>> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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 = {
|
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
|
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...
|
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;
| |
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)
{
}
|
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?
|
by: Pallav singh |
last post by:
How can we justify that initializer list is better in performance
than assignment list in constructor of C++ ??
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |