does this compile and run

Discussion in 'C Programming' started by gdotone@gmail.com, Aug 15, 2013.

  1. Guest

    #include <stdio.h>


    struct point
    {
    int x, y;
    };


    int main(int argc, const char * argv[])
    {
    struct point someArray[3] = { {0,1}, {2,3}, {0,0} };

    printf("%d %d", someArray[0].x, someArray[0].y);


    return 0;
    }


    with no errors or warnings.

    i'm getting errors using gcc.
    in Xcode, it compiles and runs fine.
     
    , Aug 15, 2013
    #1
    1. Advertising

  2. Guest

    On Thursday, August 15, 2013 11:48:04 AM UTC-4, wrote:
    > #include <stdio.h>
    >
    >
    >
    >
    >
    > struct point
    >
    > {
    >
    > int x, y;
    >
    > };
    >
    >
    >
    >
    >
    > int main(int argc, const char * argv[])
    >
    > {
    >
    > struct point someArray[3] = { {0,1}, {2,3}, {0,0} };
    >
    >
    >
    > printf("%d %d", someArray[0].x, someArray[0].y);
    >
    >
    >
    >
    >
    > return 0;
    >
    > }
    >
    >
    >
    >
    >
    > with no errors or warnings.
    >
    >
    >
    > i'm getting errors using gcc.
    >
    > in Xcode, it compiles and runs fine.


    the errors i get using gcc:

    1:error: expected '=', ' ,', ':', 'asm' or '_attribute_' before '<' token
    : In function "main':
    12: error: array type has incomplete element type
    14: warning: implicit declaration of function 'printf'
    12: warning: unused variable 'someArray'

    sorry i did not include the error messages and warnings initially

    thanks everyone.
     
    , Aug 15, 2013
    #2
    1. Advertising

  3. James Kuyper Guest

    On 08/15/2013 11:48 AM, wrote:
    > #include <stdio.h>
    >
    >
    > struct point
    > {
    > int x, y;
    > };
    >
    >
    > int main(int argc, const char * argv[])
    > {
    > struct point someArray[3] = { {0,1}, {2,3}, {0,0} };
    >
    > printf("%d %d", someArray[0].x, someArray[0].y);


    "Whether the last line requires a terminating new-line character is
    implementation-defined. ... Data read in from a text stream will
    necessarily compare equal to the data that were earlier written out to
    that stream only if ... the last character is a new-line character."
    (7.21.2p2)

    In general, the safest approach is to assume that the last line of
    output to a text stream does require a terminating new-line character,
    so change that to "%d %d\n".

    > return 0;
    > }
    >
    >
    > with no errors or warnings.
    >
    > i'm getting errors using gcc.


    What kind of errors? You're like a patient who goes to the doctor saying
    "I'm sick", without bothering to explain what his symptoms were.

    I've compiled it using gcc without any error messages. The problem I've
    pointed out above could, in principle, be the cause of arbitrarily bad
    problems. However, it is quite commonly completely harmless, and that is
    in fact the case on my system - but yours might be different. Please
    give us the exact command line that you used to run gcc, and the exact
    wording of any messages it printed out while compiling your code. If you
    didn't get any errors until you actually ran the problem, show us the
    exact results you got when you ran it.
     
    James Kuyper, Aug 15, 2013
    #3
  4. James Harris Guest

    <> wrote in message
    news:...
    > On Thursday, August 15, 2013 11:48:04 AM UTC-4, wrote:
    >> #include <stdio.h>
    >>
    >>
    >>
    >>
    >>
    >> struct point
    >>
    >> {
    >>
    >> int x, y;
    >>
    >> };
    >>
    >>
    >>
    >>
    >>
    >> int main(int argc, const char * argv[])
    >>
    >> {
    >>
    >> struct point someArray[3] = { {0,1}, {2,3}, {0,0} };
    >>
    >>
    >>
    >> printf("%d %d", someArray[0].x, someArray[0].y);
    >>
    >>
    >>
    >>
    >>
    >> return 0;
    >>
    >> }
    >>
    >>
    >>
    >>
    >>
    >> with no errors or warnings.
    >>
    >>
    >>
    >> i'm getting errors using gcc.
    >>
    >> in Xcode, it compiles and runs fine.

    >
    > the errors i get using gcc:
    >
    > 1:error: expected '=', ' ,', ':', 'asm' or '_attribute_' before '<' token
    > : In function "main':
    > 12: error: array type has incomplete element type
    > 14: warning: implicit declaration of function 'printf'
    > 12: warning: unused variable 'someArray'
    >
    > sorry i did not include the error messages and warnings initially
    >
    > thanks everyone.


    I can't see anything wrong with what's pasted above but from the error
    messages it looks like the initial # sign is missing from the source or not
    being recognised for some reason. Does that make any sense when you look at
    the file? Could you have an invisible control character in the file?

    James
     
    James Harris, Aug 15, 2013
    #4
  5. James Kuyper Guest

    On 08/15/2013 12:20 PM, wrote:
    > On Thursday, August 15, 2013 11:48:04 AM UTC-4, wrote:
    >> #include <stdio.h>
    >>
    >> struct point
    >> {
    >> int x, y;
    >> };
    >>
    >> int main(int argc, const char * argv[])
    >> {
    >> struct point someArray[3] = { {0,1}, {2,3}, {0,0} };
    >>
    >> printf("%d %d", someArray[0].x, someArray[0].y);
    >>
    >> return 0;
    >> }
    >>
    >>
    >> with no errors or warnings.
    >>
    >> i'm getting errors using gcc.
    >> in Xcode, it compiles and runs fine.

    >
    > the errors i get using gcc:
    >
    > 1:error: expected '=', ' ,', ':', 'asm' or '_attribute_' before '<' token
    > : In function "main':
    > 12: error: array type has incomplete element type
    > 14: warning: implicit declaration of function 'printf'
    > 12: warning: unused variable 'someArray'
    >
    > sorry i did not include the error messages and warnings initially


    There is no '<' token in function main().
    The #include <stdio.h> directive should have inserted a declaration of
    printf() into your program, so an implicit one should neither have been
    needed nor generated.
    someArray is not unused.

    Conclusion: something is radically wrong. At a minimum, you should give
    us the actual command line you used to compile the code. Secondly, check
    to make sure that the code you've given us is EXACTLY the code that is
    present in your program. If you copied it by hand the first time - DON'T
    do that - cut and paste the full text of your program directly into your
    next message without making any modifications. If you gave us a
    simplified version of the actual program, check to see whether the
    simplified version demonstrates the same problem - if it doesn't, then
    make it less simplified, until you can show us a program that does
    demonstrate the problem.

    Make sure that the code you show us and the compilation command that you
    show us are exactly the code and exactly the command that was used when
    you got the error messages that you've shown us.

    One possibility is that your code contains some non-printing characters
    that are causing it to appear very differently to your compiler than it
    does in your message. If you're using a unix-like system, try the "od
    -c" command on your file, and show us the results.
     
    James Kuyper, Aug 15, 2013
    #5
  6. Guest

    thanks it must have been an invisible control character. i reenter the program using the vi text editor. the # was there, but i must have not copied and pasted all the text for the post.

    the program now compiles and runs fine.

    thanks.
     
    , Aug 15, 2013
    #6
  7. writes:
    > #include <stdio.h>
    >
    > struct point
    > {
    > int x, y;
    > };
    >
    > int main(int argc, const char * argv[])
    > {
    > struct point someArray[3] = { {0,1}, {2,3}, {0,0} };
    > printf("%d %d", someArray[0].x, someArray[0].y);
    > return 0;
    > }


    I see several problems that gcc doesn't complain about by default.

    argv should not be defined as "const". It's unlikely to cause any
    problems, but there's not much point in using a definition for main
    other than one of the two forms spelled out in the standard.

    argc and argc are not used (gcc warns about this if you ask it nicely).
    If you're not using command-line arguments, you can write "int
    main(void)".

    You don't print a newline at the end of your output. This can cause
    undefined behavior in some circumstances. When I run your program, I
    get "0 1" on the same line as my next shell prompt.

    You later said that the problem went away when you re-entered the
    program. If you still have the original, and if you're on a Unix-like
    system, try using "cat -A" or "cat -v" to show the entire contents of
    the file, including non-printable characters.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Working, but not speaking, for JetHead Development, Inc.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Aug 15, 2013
    #7
    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. Christian Seberino
    Replies:
    6
    Views:
    409
    Piet van Oostrum
    Nov 9, 2003
  2. Michael T. Babcock
    Replies:
    3
    Views:
    285
    Peter Hansen
    Nov 4, 2003
  3. Ellinghaus, Lance
    Replies:
    3
    Views:
    425
    Peter Hansen
    Nov 3, 2003
  4. Ellinghaus, Lance
    Replies:
    2
    Views:
    318
    Peter Hansen
    Nov 4, 2003
  5. Nagaraj
    Replies:
    1
    Views:
    883
    Lionel B
    Mar 1, 2007
Loading...

Share This Page