c code error

Thread Starter

TAKYMOUNIR

Joined Jun 23, 2008
352
i have problem with the red line it say that i missing ;
this is just swaping function
Rich (BB code):
#include <stdio.h>
#include <stdlib.h>
#define size 20
void  triple_swap(int*,int*,int*,int);
int main()
{
    int x[20]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
    int y[20]={2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2};
    int z[20]={3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3};
    int i,*px,*py,*pz;
    printf("\ntraverse the array by using for loop and array indices\n");
    printf("x[%i]={",size);
     for(i=0;i<size;i++){
    printf("%i,",x);}
    printf("\b}\n");
     printf("y[%i]={",size);
     for(i=0;i<size;i++){
    printf("%i,",y);}
    printf("\b}\n");
     printf("z[%i]={",size);
     for(i=0;i<size;i++){
    printf("%i,",z);}
    printf("\b}\n");
    px=x;
    py=y;
    pz=z;
    printf("\ntraverse the array by using for loop and pointer arithmatic\n");
    printf("x[%i]={",size);
     for(i=0;i<size;i++){
    printf("%i,",*(px+i));}
    printf("\b}\n");
     printf("y[%i]={",size);
     for(i=0;i<size;i++){
    printf("%i,",*(py+i));}
    printf("\b}\n");
    printf("z[%i]={",size);
     for(i=0;i<size;i++){
    printf("%i,",*(pz+i));}
    printf("\b}\n");
    triple_swap(x,y,z,size);
    printf("after triple_swap");
    printf("\ntraverse the array by using for loop and array indices\n");
    printf("x[%i]={",size);
     for(i=0;i<size;i++){
    printf("%i,",x);}
    printf("\b}\n");
     printf("y[%i]={",size);
     for(i=0;i<size;i++){
    printf("%i,",y);}
    printf("\b}\n");
     printf("z[%i]={",size);
     for(i=0;i<size;i++){
    printf("%i,",z);}
    printf("\b}\n");
    px=x;
    py=y;
    pz=z;
    printf("\ntraverse the array by using for loop and pointer arithmatic\n");
    printf("x[%i]={",size);
     for(i=0;i<size;i++){
    printf("%i,",*(px+i));}
    printf("\b}\n");
     printf("y[%i]={",size);
     for(i=0;i<size;i++){
    printf("%i,",*(py+i));}
    printf("\b}\n");
    printf("z[%i]={",size);
     for(i=0;i<size;i++){
    printf("%i,",*(pz+i));}
    printf("\b}\n");
    return 0;
    }
void  triple_swap(int a[],int b[],int c[],int size)
{
    int i,temp;
for (i=0;i<size;i++){
    temp=a;
    a=b;
    c=temp;
}
}
 

WBahn

Joined Mar 31, 2012
30,088
Here we go again.

What steps have you taken to try to figure this out? Any? Or do you still just think that we are your one-click debugging service?

Hint: What the compiler says is it's best guess at what the problem is. The fact that the input file fails to meet the syntax requirements means that the compiler found something it can't interpret, so it gives you the best guess it can both in terms of where the error was and what the error was. All you really know is that an error occured on or before the line that threw the error.

Another hint: In this case, the error is on the line in question.

Yet another hint: Remember, the compiler sees the code only after the preprocessor is done with it.

Oh, and if you are going to post code that you expect others to look at for you, at least extend the courtesy of formating it so that it is easy to read and determine the structure.
 

spinnaker

Joined Oct 29, 2009
7,830
Probably a missing } in the main function. If your formatting is the same in your actual code then I would give you a failing grade minus 10 for code formatting. It is horrible.

Most modern code editors have bracket matching. You place the cursor before or after the bracket, parenthesis etc. and the editor will shows it's match.

But WBahn is right. It appears that you are trying to use this forum to troubleshoot your problems but are making no effort at all to fix issues on your own.

We are happy to help but you need to do some work on your part too.

A really simple troubleshooting technique here was to strip the code out and add it back in till the error appeared. You could then isolate your problem.
 

takao21203

Joined Apr 28, 2012
3,702
Rich (BB code):
return 0;
looks incorrect to me.

Besides, what is this code good for?
Is it neccessary to use so much detail?

It is not unusual not to know how to spell something in C.
But normally you'd use as little code as possible.

Errors like that can actually relate to mistakes in a different line.
 

spinnaker

Joined Oct 29, 2009
7,830
Rich (BB code):
return 0;
looks incorrect to me.

Besides, what is this code good for?
Is it neccessary to use so much detail?

It is not unusual not to know how to spell something in C.
But normally you'd use as little code as possible.

Errors like that can actually relate to mistakes in a different line.
What is wrong with return 0; in main????
 

spinnaker

Joined Oct 29, 2009
7,830
Shouldn't this be
Rich (BB code):
return(0);
??

But this is compiler specific.
No complier I know of C or otherwise requires parenthesis around a return value. Sometimes they are added for clarity return ((x+1) *2);

If a C compiler did require them then it would be breaking the standard for return.

More than likely the OP's issue is with the curly bracket character or brace.
 

nerdegutta

Joined Dec 15, 2009
2,684
If I remember correct. It shall not be a

Rich (BB code):
return 0;
in the main function, simply because the main function is the main function and shall not return anything.

I might remember wrong...
 

spinnaker

Joined Oct 29, 2009
7,830
If I remember correct. It shall not be a

Rich (BB code):
return 0;
in the main function, simply because the main function is the main function and shall not return anything.

I might remember wrong...
main can return a value depending on the cpu. But I think the C convention allows it in general.
 

Thread Starter

TAKYMOUNIR

Joined Jun 23, 2008
352
thanks for your help it work ok now and the is i have to change size to different name
void triple_swap(int a[],int b[],int c[],int size)
i change the code to
void triple_swap(int a[],int b[],int c[],int k)
Found this:
Rich (BB code):
printf("[%i]={",size);
Is this correct?
 

spinnaker

Joined Oct 29, 2009
7,830
thanks for your help it work ok now and the is i have to change size to different name
void triple_swap(int a[],int b[],int c[],int size)
i change the code to
void triple_swap(int a[],int b[],int c[],int k)
You must have made some other change. Changing th name of the variable is not going to fix the syntax error unless "size" is a reserved word in your compiler.
 

WBahn

Joined Mar 31, 2012
30,088
You must have made some other change. Changing th name of the variable is not going to fix the syntax error unless "size" is a reserved word in your compiler.
No. This is the problem I was hinting at. Look at the #define at the top of the code. He has

#define size 20

which means that his function declaration, as seen by the compiler, is

void triple_swap(int a[],int b[],int c[],int 20)
 

spinnaker

Joined Oct 29, 2009
7,830
No. This is the problem I was hinting at. Look at the #define at the top of the code. He has

#define size 20

which means that his function declaration, as seen by the compiler, is

void triple_swap(int a[],int b[],int c[],int 20)
Ohhhhhhhhhhhhhhhhhhhhhhh


Good eyes! :)
 

WBahn

Joined Mar 31, 2012
30,088
Shouldn't this be
Rich (BB code):
return(0);
??

But this is compiler specific.
Spiniker is correct. No complying C compiler requires the return value to be enclosed in parens. The "return" is a keyword, not a function call. When you use "exit", on the other hand, you do need parens because it IS a function call.
 

WBahn

Joined Mar 31, 2012
30,088
If I remember correct. It shall not be a

Rich (BB code):
return 0;
in the main function, simply because the main function is the main function and shall not return anything.

I might remember wrong...
The "main" function here is declared as returning a value of type int, so is has to return a value.

Presently, for a hosted implementation (basically, a program running under an OS), the two "allowed" declarations for the main function are:

int main(void)

and

int main(int, char**)

But other declarations are generally allowed, unless the compiler is set to be strictly conforming, because so much legacy code exists that doesn't conform to these.
 
Top