warning: excess elements in scalar initializer

Discussion in 'C Programming' started by helmut januschka, Mar 6, 2004.

  1. 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
     
    helmut januschka, Mar 6, 2004
    #1
    1. Advertising

  2. helmut januschka

    nrk Guest

    helmut januschka wrote:

    > 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.

    > best regards
    > Helmut Januschka


    --
    Remove devnull for email
     
    nrk, Mar 6, 2004
    #2
    1. Advertising

  3. In 'comp.lang.c', "helmut januschka" <> wrote:

    > 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...)

    --
    -ed- [remove YOURBRA before answering me]
    The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=cpp
    FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
     
    Emmanuel Delahaye, Mar 6, 2004
    #3
  4. helmut januschka

    Joe Wright Guest

    Emmanuel Delahaye wrote:
    > In 'comp.lang.c', "helmut januschka" <> wrote:
    >
    >
    >>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.
    --
    Joe Wright mailto:
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
     
    Joe Wright, Mar 6, 2004
    #4
  5. In 'comp.lang.c', Joe Wright <> wrote:

    >> 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!

    --
    -ed- [remove YOURBRA before answering me]
    The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=cpp
    FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
     
    Emmanuel Delahaye, Mar 6, 2004
    #5
  6. helmut januschka

    Joe Wright Guest

    Emmanuel Delahaye wrote:
    > In 'comp.lang.c', Joe Wright <> wrote:
    >
    >
    >>>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?

    --
    Joe Wright mailto:
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
     
    Joe Wright, Mar 6, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Guy
    Replies:
    3
    Views:
    634
    Paul Guy
    May 27, 2004
  2. MattC

    Event Log Entry - excess info

    MattC, Jan 7, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    6,071
    MattC
    Jan 10, 2005
  3. =?Utf-8?B?Y2xpY2tvbg==?=

    Removing excess white space

    =?Utf-8?B?Y2xpY2tvbg==?=, Apr 6, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    17,861
    visserj
    Feb 3, 2011
  4. Clint Olsen
    Replies:
    6
    Views:
    400
    Jeff 'japhy' Pinyan
    Nov 13, 2003
  5. Mark

    Replace scalar in another scalar

    Mark, Jan 27, 2005, in forum: Perl Misc
    Replies:
    4
    Views:
    190
    Arndt Jonasson
    Jan 27, 2005
Loading...

Share This Page