declaration error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jacob Schmidt

    #1

    declaration error

    Could anyone correct the error in my logic here?:

    #include <stdio.h>
    #include <stdlib.h>
    main ()
    {
    const char message1[] = {"\nCurved portion of graph -- D.G.A.C.\
    \n A B C"};
    const char message2[] = {"\nCurved portion of graph -- R.A.C.\
    \n A D E"};
    const char message3[] = {"\nCurved portion of graph -- D.G.A.C.\
    \nA\tB\tC"};
    const char message4[] = {"\nCurved portion of graph R.A.C.\
    \nA\tD\tE"};
    char ( * const msgptr12 [] ) [] = { message1, message2 };
    char ( * const msgptr34 [] ) [] = { message3, message 4};
    /* lint gives a type mismatch error for the previous two lines. */
    char stringbuf [85];

    ...

    printf ( "%s", msgptr12 [i] );
    numbchar = sprintf (stringbuf, "%s", msgptr34 [i] );
    fwrite (stringbuf, numbchar, 1, ofptr);


    Any thoughts or suggestions will be appreciated. L e e _ S h a c k e
    l f o r d @ d o t . c a . g o v
  • Mark A. Odell

    #2
    Re: declaration error

    Jacob Schmidt <Jacob.Schmidt@ aol.com> wrote in
    news:26npb01hbr 54esel8sls0k8n2 g9bs4uok6@4ax.c om:
    [color=blue]
    > Could anyone correct the error in my logic here?:
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > main ()
    > {
    > const char message1[] = {"\nCurved portion of graph -- D.G.A.C.\
    > \n A B C"};
    > const char message2[] = {"\nCurved portion of graph -- R.A.C.\
    > \n A D E"};
    > const char message3[] = {"\nCurved portion of graph -- D.G.A.C.\
    > \nA\tB\tC"};
    > const char message4[] = {"\nCurved portion of graph R.A.C.\
    > \nA\tD\tE"};
    > char ( * const msgptr12 [] ) [] = { message1, message2 };[/color]

    message1, 2, 3, & 4 are not compile time constants. You can't do this in
    C90. Also, main returns 'int' so why not be explicit?

    --
    - Mark ->
    --

    Comment

    • Martin Dickopp

      #3
      Re: declaration error

      Jacob Schmidt <Jacob.Schmidt@ aol.com> writes:
      [color=blue]
      > Could anyone correct the error in my logic here?:
      >
      > #include <stdio.h>
      > #include <stdlib.h>
      > main ()[/color]

      Better: int main (void)
      [color=blue]
      > {
      > const char message1[] = {"\nCurved portion of graph -- D.G.A.C.\
      > \n A B C"};
      > const char message2[] = {"\nCurved portion of graph -- R.A.C.\
      > \n A D E"};
      > const char message3[] = {"\nCurved portion of graph -- D.G.A.C.\
      > \nA\tB\tC"};
      > const char message4[] = {"\nCurved portion of graph R.A.C.\
      > \nA\tD\tE"};
      > char ( * const msgptr12 [] ) [] = { message1, message2 };
      > char ( * const msgptr34 [] ) [] = { message3, message 4};
      > /* lint gives a type mismatch error for the previous two lines. */[/color]

      This declares `msgptr12' and `msgptr34' as arrays of const pointers to
      arrays of char. The elements therefore have to have type `const pointer
      to array of char'; you cannot initialize them with something that has
      type `pointer to const char'. Try this instead:

      const char * msgptr12 [] = { message1, message2 };
      const char * msgptr34 [] = { message3, message4 };

      Note, however, that `message1', `message2', `message3', and `message4'
      are not constant expressions, so you cannot use them to initialize
      arrays (unless you have a C99 compiler, but I know you don't, since
      `main ()' is invalid in C99). There are two things you can do: Either
      declare `message1' etc. with static storage duration (i.e. `static const
      char message1 [] = ...'), or assign instead of initializing:

      const char * msgptr12 [2];
      const char * msgptr34 [2];

      msgptr12 [0] = message1;
      msgptr12 [1] = message2;
      msgptr34 [0] = message3;
      msgptr34 [1] = message4;

      Martin


      --
      ,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
      / ,- ) http://www.zero-based.org/ ((_/)o o(\_))
      \ `-' `-'(. .)`-'
      `-. Debian, a variant of the GNU operating system. \_/

      Comment

      • Martin Ambuhl

        #4
        Re: declaration error

        Jacob Schmidt wrote:[color=blue]
        > Could anyone correct the error in my logic here?:
        >
        > #include <stdio.h>
        > #include <stdlib.h>
        > main ()
        > {
        > const char message1[] = {"\nCurved portion of graph -- D.G.A.C.\
        > \n A B C"};
        > const char message2[] = {"\nCurved portion of graph -- R.A.C.\
        > \n A D E"};
        > const char message3[] = {"\nCurved portion of graph -- D.G.A.C.\
        > \nA\tB\tC"};
        > const char message4[] = {"\nCurved portion of graph R.A.C.\
        > \nA\tD\tE"};
        > char ( * const msgptr12 [] ) [] = { message1, message2 };
        > char ( * const msgptr34 [] ) [] = { message3, message 4};
        > /* lint gives a type mismatch error for the previous two lines. */
        > char stringbuf [85];
        >
        > ...
        >
        > printf ( "%s", msgptr12 [i] );
        > numbchar = sprintf (stringbuf, "%s", msgptr34 [i] );
        > fwrite (stringbuf, numbchar, 1, ofptr);[/color]


        #include <stdio.h>

        int main(void)
        {
        const char message1[] = "Curved portion of graph -- D.G.A.C.\n"
        " A B C\n";
        const char message2[] = "Curved portion of graph -- R.A.C.\n"
        " A D E\n";
        const char message3[] = "Curved portion of graph -- D.G.A.C.\n"
        "A\tB\tC\n" ;
        const char message4[] = "Curved portion of graph R.A.C.\n"
        "A\tD\tE\n" ;
        const char *const msgptr12[] = { message1, message2 };
        const char *const msgptr34[] = { message3, message4 };
        char Strbuf[85];
        size_t i = 0, numbchar;


        printf("%s", msgptr12[i]);
        numbchar = sprintf(Strbuf, "%s\n", msgptr34[i]);
        fwrite(Strbuf, numbchar, 1, stdout);
        return 0;
        }

        Comment

        • Old Wolf

          #5
          Re: declaration error

          Jacob Schmidt <Jacob.Schmidt@ aol.com> wrote:[color=blue]
          >
          > Could anyone correct the error in my logic here?:
          >
          > #include <stdio.h>
          > #include <stdlib.h>
          > main ()
          > {
          > const char message1[] = {"\nCurved portion of graph -- D.G.A.C.\
          > \n A B C"};[/color]

          I think newlines in string literals are considered bad form these days
          [color=blue]
          > const char message2[] = {"\nCurved portion of graph -- R.A.C.\
          > \n A D E"};
          > char ( * const msgptr12 [] ) [] = { message1, message2 };[/color]
          [color=blue]
          > /* lint gives a type mismatch error for the previous two lines. */[/color]

          'msgptr12' is an array of const pointers to arrays (of unspecified size)
          of char.
          'message1' is the name of an array of const char. This is not a
          pointer to (non-const) char. You need the '&' sign to take its
          address, and you need to make msgptr12 point to arrays of const char:

          const char (* const msgptr12[]) [] = { &message1, &message2 };

          This compiles OK now but it is not terribly useful.
          msgptr12[0] has type 'pointer to array (of unknown size) of const char'.
          This is an incomplete type, so you cannot actually dereference that
          pointer, so you can't access the chars in it safely.
          The expression *msgptr12[0] would be a syntax error.
          [color=blue]
          > printf ( "%s", msgptr12 [i] );[/color]

          Undefined behaviour - %s expects a pointer to char, but you gave it a
          pointer to array. You were (un)lucky that you chose a variadic function,
          if you tried:
          puts(msgptr12[i])
          you would get a compiler warning.

          If you want msgptr12's members to point to complete arrays, then the
          arrays all have to be the same size and you have to specify that, eg:

          const char (* const msgptr12[]) [40] = { &message1, &message2 };
          printf("%s", *msgptr12[1]);

          Or, as others have suggested, you could simply use an array of
          pointers to char (but this was a useful exercise in understanding
          pointers to arrays).

          Comment

          • Ben Pfaff

            #6
            Re: declaration error

            oldwolf@inspire .net.nz (Old Wolf) writes:
            [color=blue]
            > Jacob Schmidt <Jacob.Schmidt@ aol.com> wrote:[color=green]
            >>
            >> main ()
            >> {
            >> const char message1[] = {"\nCurved portion of graph -- D.G.A.C.\
            >> \n A B C"};[/color]
            >
            > I think newlines in string literals are considered bad form these days[/color]

            Unescaped new-lines are not allowed in string literals, but this
            new-line is immediately preceded by a \ that splices the two
            lines together, so it's okay. (Another example in my .sig below,
            simply by accident.)
            --
            int main(void){char p[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
            \n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int putchar(\
            );while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof p)i-=sizeof p-1;putchar(p[i]\
            );}return 0;}

            Comment

            • Dave Thompson

              #7
              Re: declaration error

              On 2 Jun 2004 14:32:10 -0700, oldwolf@inspire .net.nz (Old Wolf) wrote:
              [color=blue]
              > Jacob Schmidt <Jacob.Schmidt@ aol.com> wrote:[/color]
              [color=blue][color=green]
              > > const char message1[] = <snip> [message 2 similar]
              > > char ( * const msgptr12 [] ) [] = { message1, message2 };[/color]
              >[color=green]
              > > /* lint gives a type mismatch error for the previous two lines. */[/color]
              >
              > 'msgptr12' is an array of const pointers to arrays (of unspecified size)
              > of char.
              > 'message1' is the name of an array of const char. This is not a
              > pointer to (non-const) char. You need the '&' sign to take its
              > address, and you need to make msgptr12 point to arrays of const char:
              >
              > const char (* const msgptr12[]) [] = { &message1, &message2 };
              >[/color]
              Agree so far.
              [color=blue]
              > This compiles OK now but it is not terribly useful.
              > msgptr12[0] has type 'pointer to array (of unknown size) of const char'.
              > This is an incomplete type, so you cannot actually dereference that
              > pointer, so you can't access the chars in it safely.[/color]

              To be clear, 'array of unknown size (of anything)' is the incomplete
              type; 'pointer to X' is complete even if X is incomplete. But I think
              you _can_ dereference it: this is a corner case, where it is allowed
              to have a pointer to incomplete and dereference it to form an lvalue
              of incomplete type; you explicitly musn't fetch (convert to rvalue) or
              store (assign) such, but AFAICS if as here it is an array you can let
              it decay to pointer to element and legally use that, within the bound
              of the array object it points "to" (rather, to the base element of).

              What you can't have is an actual (defined) object of incomplete type,
              and in C99 you explicitly can't have a declaration for 'array of
              incomplete' (which this example isn't, mind you) whereas in C89 _as a
              function parameter_ an implementation _might_ let you declare array of
              incomplete and "rewrite" it to pointer to incomplete which is OK.
              [color=blue]
              > The expression *msgptr12[0] would be a syntax error.
              >[/color]
              The standard doesn't define 'syntax error' but I think it is usually
              taken to mean 'violation of any syntax rule', which this is not;
              if anything it would be a constraint violation -- also a required
              diagnostic, but a separate category. And I say not even that.
              [color=blue][color=green]
              > > printf ( "%s", msgptr12 [i] );[/color]
              >
              > Undefined behaviour - %s expects a pointer to char, but you gave it a
              > pointer to array. You were (un)lucky that you chose a variadic function,
              > if you tried:
              > puts(msgptr12[i])
              > you would get a compiler warning.
              >[/color]
              Agree here. In fact gcc will even unrequiredly warn on the printf.
              [color=blue]
              > If you want msgptr12's members to point to complete arrays, then the
              > arrays all have to be the same size and you have to specify that, eg:
              >
              > const char (* const msgptr12[]) [40] = { &message1, &message2 };
              > printf("%s", *msgptr12[1]);
              >
              > Or, as others have suggested, you could simply use an array of
              > pointers to char (but this was a useful exercise in understanding
              > pointers to arrays).[/color]

              Yes.

              - David.Thompson1 at worldnet.att.ne t

              Comment

              • Old Wolf

                #8
                Re: declaration error

                Dave Thompson <david.thompson 1@worldnet.att. net> wrote:[color=blue]
                > oldwolf@inspire .net.nz (Old Wolf) wrote:
                >[color=green]
                > > Jacob Schmidt <Jacob.Schmidt@ aol.com> wrote:[/color]
                >[color=green][color=darkred]
                > > > const char message1[] = <snip> [message 2 similar][/color]
                > > const char (* const msgptr12[]) [] = { &message1, &message2 };[/color]
                >[color=green]
                > > msgptr12[0] has type 'pointer to array (of unknown size) of const char'.
                > > This is an incomplete type, so you cannot actually dereference that
                > > pointer, so you can't access the chars in it safely.[/color]
                >
                > To be clear, 'array of unknown size (of anything)' is the incomplete
                > type; 'pointer to X' is complete even if X is incomplete. But I think
                > you _can_ dereference it: this is a corner case, where it is allowed
                > to have a pointer to incomplete and dereference it to form an lvalue
                > of incomplete type; you explicitly musn't fetch (convert to rvalue) or
                > store (assign) such, but AFAICS if as here it is an array you can let
                > it decay to pointer to element and legally use that, within the bound
                > of the array object it points "to" (rather, to the base element of).[/color]

                Thanks for the clarifications.

                Comment

                Working...