does this compile and run

G

gdotone

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

gdotone

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

James Kuyper

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

James Harris

#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
 
J

James Kuyper

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

gdotone

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

Keith Thompson

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

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

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top