"Ahmed Shabana" <[EMAIL PROTECTED]> wrote:
> Subject: has this any syntax error ??
Don't ask your question in the subject line. In any case,
most compilers can answer the question for you.
Your post reaks of homework assignment to me.
Is there any reason you're limiting the question to
syntax errors?
> #include<stdio.h>
>
> // My implementation of sort
Syntax error under C90.
> #define LEN ( sizeof(x) / sizeof(int) )
A much more robust macro is...
#define countof(x) ((size_t)(sizeof(x)/sizeof*(x)))
> main()
Syntax error in C99 and C++. Try...
int main(void)
> {
> int x[] = {5,3,2,7,8,9} ;
> int index[LEN] ;
int index[countof(x)];
Notice how this macro works even if you one day change
the type of x? Note also that size_t is a better index
variable than int in most cases.
That said, it's not clear why you defined a variable
that you never use.
> int *tmp, i, j, c = 0;
Why make tmp a pointer? Writing tmp = 0 has to be easier
to read and write than tmp = &x[0].
> int *sorted[LEN] ;
>
> if (x[0] > x[1] )
> tmp = &x[0] ;
> else
> tmp = &x[1] ;
> for ( i = 0; i++; i = LEN-c ){
Valid syntax, but garbage nonetheless.
> for ( j = 0; ; j = c ){
Valid syntax, but garbage nonetheless.
> if ( &x[i] != sorted[j++] || c == 0 )
The contents of 'sorted' are uninitialised (garbage) to begin
with. Comparing an element with x[i] will invoke undefined
behaviour.
> if ( *tmp > x[i] )
> ;
> else tmp = &x[i] ;
> }
> sorted[LEN-c] = tmp ;
If c is 0, this will tread on memory you don't own.
> ++c ;
>
> }
> for (i = 0; i++; i = LEN )
Valid syntax, but garbage nonetheless.
> printf( "%d\n",*sorted[i] );
> }
>
> PLZ TRY IT YOUR SELF AND THEN ANSWER ME
Funny, that's the exact thing I was going to ask you to do.
--
Peter