warning: excess elements in scalar initializer

  • Thread starter helmut januschka
  • Start date
H

helmut januschka

hi !,

Following Code gives me following error's
warning: excess elements in scalar initializer


char *functionname = {

"abs", "sqrt", "sin", "cos", "atan", "log", "exp", "\0"

};

best regards
Helmut Januschka
 
N

nrk

helmut said:
hi !,

Following Code gives me following error's
warning: excess elements in scalar initializer


char *functionname = {

"abs", "sqrt", "sin", "cos", "atan", "log", "exp", "\0"

};

That's because your initializer is for an array of pointers to char, while
functionname is just a pointer to char (a scalar). What you want, probably
is:

char *functionname[] =
{ "abs", "sqrt", "sin", "cos", "atan", "log", "exp", "\0", };

-nrk.
 
E

Emmanuel Delahaye

helmut januschka said:
warning: excess elements in scalar initializer

char *functionname = {

"abs", "sqrt", "sin", "cos", "atan", "log", "exp", "\0"

};

Sure, you want an array :

char const *functionname[] =
{
"abs", "sqrt", "sin", "cos", "atan", "log", "exp", NULL
};

(The NULL thing is probably useless...)
 
J

Joe Wright

Emmanuel said:
helmut januschka said:
warning: excess elements in scalar initializer

char *functionname = {

"abs", "sqrt", "sin", "cos", "atan", "log", "exp", "\0"

};


Sure, you want an array :

char const *functionname[] =
{
"abs", "sqrt", "sin", "cos", "atan", "log", "exp", NULL
};

(The NULL thing is probably useless...)
Not at all. The NULL thing is more useful than the "\0" thing to be
sure. Consider..

for (i = 0; functionname != NULL; ++i)
puts(functionname);

...or..

for (i = 0; *functionname != '\0'; ++i)
puts(functionname);

I like the NULL treatment best.
 
E

Emmanuel Delahaye

In 'comp.lang.c' said:
char const *functionname[] =
{
"abs", "sqrt", "sin", "cos", "atan", "log", "exp", NULL
};

(The NULL thing is probably useless...)
Not at all. The NULL thing is more useful than the "\0" thing to be
sure. Consider..

for (i = 0; functionname != NULL; ++i)
puts(functionname);

..or..

for (i = 0; *functionname != '\0'; ++i)
puts(functionname);

I like the NULL treatment best.


The old C-string vs Pascal-string discussion is back!
 
J

Joe Wright

Emmanuel said:
char const *functionname[] =
{
"abs", "sqrt", "sin", "cos", "atan", "log", "exp", NULL
};

(The NULL thing is probably useless...)

Not at all. The NULL thing is more useful than the "\0" thing to be
sure. Consider..

for (i = 0; functionname != NULL; ++i)
puts(functionname);

..or..

for (i = 0; *functionname != '\0'; ++i)
puts(functionname);

I like the NULL treatment best.



The old C-string vs Pascal-string discussion is back!

I don't think my response did it but yours now does. The Pascal 'string'
has a leading unsigned char (0..255) to indicate the length of the
string. How would Pascal present a 300 character string?
 

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,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top