Talk:Syntax error

From Wikipedia, the free encyclopedia

This is the talk page for discussing improvements to the Syntax error article.

Article policies

[edit] Logic error

This was in the article as a syntax error, but I think it would compile okay:


Example of a syntaxlogic error in C:

   static char *days[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
   int i;
   for(i=0;i<7;i++) printf(--~~~~Insert formula here<s>Strike-through text</s>"Day %d: %d\n",i,days[i]);


In the above example, days[i]'s memory location in integer form is printed and not the string it contains, producing unexpected results but not failing the program.

Correct code: to output correct result instead of second %d conversion must be %s conversion sign: printf("Day %d: %s\n",i,days[i]);.


Let's come up with a better example of a syntax error. --Uncle Ed 17:36, 16 November 2006 (UTC)

Hmm. The above was a logic error caused by a mistake in syntax. This, however, is not what is usually meant by a syntax error, which almost always is caught by the compiler and which is either flagged as "not really good" in some way.
We need to distinguish between "caught" syntax errors and easily-misused features like indirection and dereferencing. Because C is so close to assembly language, it allows the use of memory pointers.
I'd prefer to discuss the hazards of pointer logic in another article. It's way to deep (complex?) a concept for a general example of a syntax error. Perhaps an entire article just devoted to dereferencing errors would be good? I mean, whether to use the * or @ and what happens when you use (or omit) a sign wrongly? --Uncle Ed 15:14, 17 November 2006 (UTC)

Insert non-formatted text here