For starters variable length arrays as the array declared in your function
int arr1[s] = {};
is not a standard C++ feature. And moreover in C where variable length arrays exist you may not initialize them in their declarations.
Moreover if the source array contains only one or two different elements then the value of the variable k will be incorrect and equal to either 0 (instead of 1) or 1 (instead of 2).
Apart from this the function shall not output anything. It is the caller of the function decides whether to output the sub-array of unique elements. And as the second parameter is passed by reference in C meaning then it shall be changed within the function.
There is standard algorithm std::unique that can be used to do the task. Here is a demonstrative program.
#include <iostream>
#include <iterator>
#include <algorithm>
int main()
{
int a[] = { 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5 };
auto last = std::unique( std::begin( a ), std::end( a ) );
for ( auto first = std::begin( a ); first != last; ++ first )
{
std::cout << *first << ' ';
}
std::cout << '\n';
return 0;
}
The program output is
1 2 3 4 5
If you want to write a similar function for arrays yourself using pointers within the function then it can look for example the following way
#include <iostream>
template <typename T>
size_t removeDuplicates( T *a, size_t n )
{
T *dest = a;
if ( n != 0 )
{
++dest;
for ( T *current = a; ++current != a + n; )
{
if ( *current != *( dest - 1 ) )
{
if ( dest != current )
{
*dest = *current;
}
++dest;
}
}
}
return dest - a;
}
int main()
{
int a[] = { 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5 };
const size_t N = sizeof( a ) / sizeof( *a );
size_t last = removeDuplicates( a, N );
for ( size_t first = 0; first != last; ++first )
{
std::cout << a[first] << ' ';
}
std::cout << '\n';
return 0;
}
Again the program output is
1 2 3 4 5
*(p + i)rather than justp[i]itself?int arr1[s]is not valid C++, although some compilers may support it as a non-standard extension (keyword: VLA). If you want a dynamically allocated array, consider using a vector. (Note: this is not the problem, if it compiles, it works, it's just not something you should be relying on)