warning: excess elements in scalar initializer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • helmut januschka

    warning: excess elements in scalar initializer

    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


  • nrk

    #2
    Re: warning: excess elements in scalar initializer

    helmut januschka wrote:
    [color=blue]
    > hi !,
    >
    > Following Code gives me following error's
    > warning: excess elements in scalar initializer
    >
    >
    > char *functionname = {
    >
    > "abs", "sqrt", "sin", "cos", "atan", "log", "exp", "\0"
    >
    > };
    >[/color]

    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.
    [color=blue]
    > best regards
    > Helmut Januschka[/color]

    --
    Remove devnull for email

    Comment

    • Emmanuel Delahaye

      #3
      Re: warning: excess elements in scalar initializer

      In 'comp.lang.c', "helmut januschka" <klewan@chello. at> wrote:
      [color=blue]
      > warning: excess elements in scalar initializer
      >
      > char *functionname = {
      >
      > "abs", "sqrt", "sin", "cos", "atan", "log", "exp", "\0"
      >
      > };[/color]

      Sure, you want an array :

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

      (The NULL thing is probably useless...)

      --
      -ed- emdelYOURBRA@no os.fr [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/

      Comment

      • Joe Wright

        #4
        Re: warning: excess elements in scalar initializer

        Emmanuel Delahaye wrote:[color=blue]
        > In 'comp.lang.c', "helmut januschka" <klewan@chello. at> wrote:
        >
        >[color=green]
        >>warning: excess elements in scalar initializer
        >>
        >>char *functionname = {
        >>
        >> "abs", "sqrt", "sin", "cos", "atan", "log", "exp", "\0"
        >>
        >>};[/color]
        >
        >
        > Sure, you want an array :
        >
        > char const *functionname[] =
        > {
        > "abs", "sqrt", "sin", "cos", "atan", "log", "exp", NULL
        > };
        >
        > (The NULL thing is probably useless...)
        >[/color]
        Not at all. The NULL thing is more useful than the "\0" thing to be
        sure. Consider..

        for (i = 0; functionname[i] != NULL; ++i)
        puts(functionna me[i]);

        ...or..

        for (i = 0; *functionname[i] != '\0'; ++i)
        puts(functionna me[i]);

        I like the NULL treatment best.
        --
        Joe Wright mailto:joewwrig ht@comcast.net
        "Everything should be made as simple as possible, but not simpler."
        --- Albert Einstein ---

        Comment

        • Emmanuel Delahaye

          #5
          Re: warning: excess elements in scalar initializer

          In 'comp.lang.c', Joe Wright <joewwright@com cast.net> wrote:
          [color=blue][color=green]
          >> char const *functionname[] =
          >> {
          >> "abs", "sqrt", "sin", "cos", "atan", "log", "exp", NULL
          >> };
          >>
          >> (The NULL thing is probably useless...)
          >>[/color]
          > Not at all. The NULL thing is more useful than the "\0" thing to be
          > sure. Consider..
          >
          > for (i = 0; functionname[i] != NULL; ++i)
          > puts(functionna me[i]);
          >
          > ..or..
          >
          > for (i = 0; *functionname[i] != '\0'; ++i)
          > puts(functionna me[i]);
          >
          > I like the NULL treatment best.[/color]

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

          --
          -ed- emdelYOURBRA@no os.fr [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/

          Comment

          • Joe Wright

            #6
            Re: warning: excess elements in scalar initializer

            Emmanuel Delahaye wrote:[color=blue]
            > In 'comp.lang.c', Joe Wright <joewwright@com cast.net> wrote:
            >
            >[color=green][color=darkred]
            >>>char const *functionname[] =
            >>>{
            >>> "abs", "sqrt", "sin", "cos", "atan", "log", "exp", NULL
            >>>};
            >>>
            >>>(The NULL thing is probably useless...)
            >>>[/color]
            >>
            >>Not at all. The NULL thing is more useful than the "\0" thing to be
            >>sure. Consider..
            >>
            >>for (i = 0; functionname[i] != NULL; ++i)
            >> puts(functionna me[i]);
            >>
            >>..or..
            >>
            >>for (i = 0; *functionname[i] != '\0'; ++i)
            >> puts(functionna me[i]);
            >>
            >>I like the NULL treatment best.[/color]
            >
            >
            > The old C-string vs Pascal-string discussion is back!
            >[/color]
            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:joewwrig ht@comcast.net
            "Everything should be made as simple as possible, but not simpler."
            --- Albert Einstein ---

            Comment

            Working...