c code error

Discussion in 'Programmer's Corner' started by TAKYMOUNIR, Nov 25, 2012.

  1. TAKYMOUNIR

    Thread Starter Active Member

    Jun 23, 2008
    351
    1
    i have problem with the red line it say that i missing ;
    this is just swaping function
    Code ( (Unknown Language)):
    1. #include <stdio.h>
    2. #include <stdlib.h>
    3. #define size 20
    4. void  triple_swap(int*,int*,int*,int);
    5. int main()
    6. {
    7.     int x[20]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
    8.     int y[20]={2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2};
    9.     int z[20]={3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3};
    10.     int i,*px,*py,*pz;
    11.     printf("\ntraverse the array by using for loop and array indices\n");
    12.     printf("x[%i]={",size);
    13.      for(i=0;i<size;i++){
    14.     printf("%i,",x[i]);}
    15.     printf("\b}\n");
    16.      printf("y[%i]={",size);
    17.      for(i=0;i<size;i++){
    18.     printf("%i,",y[i]);}
    19.     printf("\b}\n");
    20.      printf("z[%i]={",size);
    21.      for(i=0;i<size;i++){
    22.     printf("%i,",z[i]);}
    23.     printf("\b}\n");
    24.     px=x;
    25.     py=y;
    26.     pz=z;
    27.     printf("\ntraverse the array by using for loop and pointer arithmatic\n");
    28.     printf("x[%i]={",size);
    29.      for(i=0;i<size;i++){
    30.     printf("%i,",*(px+i));}
    31.     printf("\b}\n");
    32.      printf("y[%i]={",size);
    33.      for(i=0;i<size;i++){
    34.     printf("%i,",*(py+i));}
    35.     printf("\b}\n");
    36.     printf("z[%i]={",size);
    37.      for(i=0;i<size;i++){
    38.     printf("%i,",*(pz+i));}
    39.     printf("\b}\n");
    40.     triple_swap(x,y,z,size);
    41.     printf("after triple_swap");
    42.     printf("\ntraverse the array by using for loop and array indices\n");
    43.     printf("x[%i]={",size);
    44.      for(i=0;i<size;i++){
    45.     printf("%i,",x[i]);}
    46.     printf("\b}\n");
    47.      printf("y[%i]={",size);
    48.      for(i=0;i<size;i++){
    49.     printf("%i,",y[i]);}
    50.     printf("\b}\n");
    51.      printf("z[%i]={",size);
    52.      for(i=0;i<size;i++){
    53.     printf("%i,",z[i]);}
    54.     printf("\b}\n");
    55.     px=x;
    56.     py=y;
    57.     pz=z;
    58.     printf("\ntraverse the array by using for loop and pointer arithmatic\n");
    59.     printf("x[%i]={",size);
    60.      for(i=0;i<size;i++){
    61.     printf("%i,",*(px+i));}
    62.     printf("\b}\n");
    63.      printf("y[%i]={",size);
    64.      for(i=0;i<size;i++){
    65.     printf("%i,",*(py+i));}
    66.     printf("\b}\n");
    67.     printf("z[%i]={",size);
    68.      for(i=0;i<size;i++){
    69.     printf("%i,",*(pz+i));}
    70.     printf("\b}\n");
    71.     return 0;
    72.     }
    73. [COLOR=red]void  triple_swap(int a[],int b[],int c[],int size)
    74. [/COLOR]{
    75.     int i,temp;
    76. for (i=0;i<size;i++){
    77.     temp=a[i];
    78.     a[i]=b[i];
    79.     c[i]=temp;
    80. }
    81. }
    82. [/i][/i][/i][/i][/i][/i][/i][/i][/i][/i]
     
  2. WBahn

    Moderator

    Mar 31, 2012
    17,720
    4,788
    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.
     
  3. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    991
    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.
     
  4. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    Code ( (Unknown Language)):
    1. 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.
     
  5. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    991
    What is wrong with return 0; in main????
     
  6. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    Shouldn't this be
    Code ( (Unknown Language)):
    1. return(0);
    ??

    But this is compiler specific.
     
    Eric007 likes this.
  7. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    991
    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.
     
  8. nerdegutta

    Moderator

    Dec 15, 2009
    2,515
    785
    If I remember correct. It shall not be a

    Code ( (Unknown Language)):
    1. return 0;
    in the main function, simply because the main function is the main function and shall not return anything.

    I might remember wrong...
     
  9. nerdegutta

    Moderator

    Dec 15, 2009
    2,515
    785
    Found this:
    Code ( (Unknown Language)):
    1.  
    2. printf("[%i]={",size);
    3.  
    Is this correct?
     
    TAKYMOUNIR likes this.
  10. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    991
    main can return a value depending on the cpu. But I think the C convention allows it in general.
     
  11. TAKYMOUNIR

    Thread Starter Active Member

    Jun 23, 2008
    351
    1
    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)
     
  12. nerdegutta

    Moderator

    Dec 15, 2009
    2,515
    785
    OK, so I was right, and wrong. Systemdependable. Good to know.
     
  13. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    991
    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.
     
  14. TAKYMOUNIR

    Thread Starter Active Member

    Jun 23, 2008
    351
    1
    thanks it works now i just change size in function to
    void triple_swap(int a[],int b[],int c[],int k)
    but i do not know why i can not use size
    thanks
     
  15. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    991
    Size is probably a reserved word. I knew sizeof was so it might just be your particular compiler.
     
  16. WBahn

    Moderator

    Mar 31, 2012
    17,720
    4,788
    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)
     
  17. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    991
    Ohhhhhhhhhhhhhhhhhhhhhhh


    Good eyes! :)
     
  18. WBahn

    Moderator

    Mar 31, 2012
    17,720
    4,788
    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.
     
  19. WBahn

    Moderator

    Mar 31, 2012
    17,720
    4,788
    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.
     
  20. WBahn

    Moderator

    Mar 31, 2012
    17,720
    4,788
    No problem with this. What raised a red flag for you?
     
Loading...