0

hello
i wonder if there is a way to return an array in function
like in java lang for example: int[] ret (int i,int j) {
int[] a = (i,j} ;
return a ;
}
or can i ewturn a pointer to a new array?

i need it for function i wrote
the function reduces duplicate values in array

void noDup[] ( int A[] , int n ) {

  int i , j , k , s ;

  k = n ;

  for ( i=0 ; i<n ; i++ ) {

    for ( j=0 ; j<n ; j++ )

      if ( A[i] == A[j] ) {

           for ( s=j ; s<n-1 ; s++ )

             A[s] = A[s+1] ;

        k-- ;

        } // end if

  } // end for

  int tmp[k] ;

  for ( i=0 ; i<k ; i++ )

    tmp[i] = A[i] ;

  A = tmp ;

  } // noDup end

thank you

3
Contributors
3
Replies
4
Views
9 Years
Discussion Span
Last Post by Duoas
0

You can return a pointer to the allocated array

int* foo()
{
    int* array = malloc( number_elements * sizeof(int) );
    // do something with the array


    return array;
}
0

Don't forget that you'll have to free() the array when you are done with it.

int *array_without_doubles = remove_doubles( array_with_doubles );
print_array( array_without_doubles );
free( array_without_doubles );

Good luck.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.