Need a bit Pointer help
May 2, 2017 at 10:59am UTC
Hello every1.. i am trying to sort an array of n elements through pointers. While compiling it gives me this: Compiler & Linker output: Undefined reference to 'sort(int* ,int[] ,int)' What m i doing wrong? Here is the code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#include <iostream.h>
void sort(int * , int [] ,int ); //Function prototype
main()
{
int size , i , *ptr;
cout<<"Specify the size of array please:\n" ;
cin>>size;
int array[size];
cout<<"Please enter the elements into the array.\n" ;
for (i=0; i<size; i++)
{
cout<<"Element-" <<i<<": " ;
cin>>array[i];
}
cout<<"**** The array before sorting is **** " ;
for (i=0; i<size; i++)
{
cout<<array[i]<<" " ;
}
ptr = &array[0];
sort(ptr ,array ,size); //Function call
}
void sort(int *ptr , int array, int size)
{
int temp , i , j;
temp = 0;
for (i=0; i<size; i++)
{
for (j=i+1; j<size; j++)
{
if (*ptr>*(ptr+1))
{
temp = *ptr;
*ptr = *(ptr + 1);
*(ptr + 1) = temp;
}
}
}
}
Last edited on May 2, 2017 at 11:01am UTC
May 2, 2017 at 11:07am UTC
The definition and declarartion of your sort function doesn't match.
1 2
void sort(int * , int [] ,int ); //Function prototype
void sort(int *ptr , int array, int size)
May 4, 2017 at 11:59am UTC
Thank you so much...
Topic archived. No new replies allowed.