Thread: Code error

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0

    Code error


    i have problem with the red line it say that i missing ; when i do compile
    this is just swaping function
    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[i]);}
        printf("\b}\n");
         printf("y[%i]={",size);
         for(i=0;i<size;i++){
        printf("%i,",y[i]);}
        printf("\b}\n");
         printf("z[%i]={",size);
         for(i=0;i<size;i++){
        printf("%i,",z[i]);}
        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[i]);}
        printf("\b}\n");
         printf("y[%i]={",size);
         for(i=0;i<size;i++){
        printf("%i,",y[i]);}
        printf("\b}\n");
         printf("z[%i]={",size);
         for(i=0;i<size;i++){
        printf("%i,",z[i]);}
        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[i];
        a[i]=b[i];
        c[i]=temp;
    }
    }
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,377
    Rep Power
    1871
    Code:
    #define size 20
    ///
    void triple_swap(int a[], int b[], int c[], int size)
    What the compiler sees is
    void triple_swap(int a[], int b[], int c[], int 20)

    The usual convention is for #define constants to be written in upper case, so they don't accidentally match variable names in the code.

    Eg.
    #define SIZE 20
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo