-2

I am getting the following error while compiled using gdb:

/tmp/ccYfysv0.o: In function `main':
main.c:(.text+0xf1): undefined reference to `reverse'
collect2: error: ld returned 1 exit status

other answers say it might be due to mispelling of the function name, but here is the code that I am trying to compile.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int reverse(int,int,int*);
int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int temp,n,i;
    scanf("%d",&n);
    int a[n];
    for(i=0;i<n;i++)
    scanf("%d",&a[i]);
    int y = reverse(n,(int)0,&a[0]);

    int reverse(int o,int k,int* p){ 
        
        temp = *(p+k);
        *(p+k)=*(p+o);
        *(p+o) = temp;
        o=o-1;k=k+1;
       
        if(o==k)
        {
           return 0;
        }
        else if((k+1)==o){
            temp = *(p+k);
            *(p+k)=*(p+o);
            *(p+o) = temp;
             return 0;
        }
        else{
          reverse(o,k,p);
        }
    }
    for(i=0;i<n;i++)
    printf("%d",a[i]);

    return 0;
}

But when I compile it with g++, I get the following error:

expected a ';'

could someone please help me out with this problem.

roshan
  • 1
  • 7
    You can't define functions inside of functions. Move the definition of `reverse()` somewhere outside `main()` – drescherjm Jan 06 '21 at 19:37
  • 1
    `I am getting the following error while compiled using gdb` - really, you use gdb to compile the code? – Arkadiusz Drabczyk Jan 06 '21 at 19:41
  • You cant define static arrays (`int a[n];`) at runtime like that. For this use either `new/ delete` or `malloc/ free` to allocate the memory (`size = sizeof(int) * n`). – D-RAJ Jan 06 '21 at 19:43

2 Answers2

0

You can't define reverse inside of main like that. Move it out separate from main, something on this general order:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int reverse(int o,int k,int* p){ 
    
    temp = *(p+k);
    *(p+k)=*(p+o);
    *(p+o) = temp;
    o=o-1;k=k+1;
    
    if(o==k)
    {
        return 0;
    }
    else if((k+1)==o){
        temp = *(p+k);
        *(p+k)=*(p+o);
        *(p+o) = temp;
        return 0;
    }
    else{
        reverse(o,k,p);
    }
}

int main() {
    int temp,n,i;

    scanf("%d",&n);
    int a[n];

    for(i=0;i<n;i++)
        scanf("%d",&a[i]);

    int y = reverse(n,(int)0,&a[0]);

    for(i=0;i<n;i++)
        printf("%d",a[i]);

    return 0;
}
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • Segmentation fault waiting to happen because of `int a[n];`. – D-RAJ Jan 06 '21 at 19:44
  • 2
    @DhirajWishal true, given a large enough `n`, but better if you call it Undefined Behaviour. All sorts of evil smurf can happen (or not happen) when the stack overflows and a segfault is one of the nicer possibilities. – user4581301 Jan 06 '21 at 20:08
  • 1
    @DhirajWishal: Yes, but it's written in C, and the fact that C includes such a poorly considered "feature" (and it's not something ancient and deprecated either) seems to indicate that C uses simply don't care much about such considerations. – Jerry Coffin Jan 06 '21 at 23:14
0

You declared a function reverse(), but you did not define it outside of main() (you can't define a function inside of another function, like you did), so the linker can't find the implementation of reverse() to satisfy main's call to it.

Also, your reverse() function is missing a return statement in the final else block.

Try this instead:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int reverse(int,int,int*);

int main()
{
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int n,i;
    scanf("%d",&n);
    int a[n];
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);

    int y = reverse(n,0,&a[0]);

    for(i=0;i<n;i++)
        printf("%d",a[i]);

    return 0;
}

int reverse(int o,int k,int* p)
{
    int temp = *(p+k);
    *(p+k)=*(p+o);
    *(p+o) = temp;
    o=o-1;k=k+1;
       
    if(o==k)
    {
        return 0;
    }
    else if((k+1)==o){
        temp = *(p+k);
        *(p+k)=*(p+o);
        *(p+o) = temp;
        return 0;
    }
    else{
        return reverse(o,k,p);
    }
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770