User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 456,610 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,487 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 372 | Replies: 3
Reply
Join Date: Nov 2007
Posts: 83
Reputation: emilio is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
emilio emilio is offline Offline
Junior Poster in Training

arrays

  #1  
Nov 10th, 2007
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
  1. void noDup[] ( int A[] , int n ) {
  2.  
  3. int i , j , k , s ;
  4.  
  5. k = n ;
  6.  
  7. for ( i=0 ; i<n ; i++ ) {
  8.  
  9. for ( j=0 ; j<n ; j++ )
  10.  
  11. if ( A[i] == A[j] ) {
  12.  
  13. for ( s=j ; s<n-1 ; s++ )
  14.  
  15. A[s] = A[s+1] ;
  16.  
  17. k-- ;
  18.  
  19. } // end if
  20.  
  21. } // end for
  22.  
  23. int tmp[k] ;
  24.  
  25. for ( i=0 ; i<k ; i++ )
  26.  
  27. tmp[i] = A[i] ;
  28.  
  29. A = tmp ;
  30.  
  31. } // noDup end

thank you
Last edited by Ancient Dragon : Nov 10th, 2007 at 12:24 pm. Reason: add code tags
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: arrays

  #2  
Nov 10th, 2007
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;
}
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Nov 2007
Posts: 83
Reputation: emilio is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
emilio emilio is offline Offline
Junior Poster in Training

Re: arrays

  #3  
Nov 10th, 2007
thanks Dragon
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 13
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: arrays

  #4  
Nov 11th, 2007
Don't forget that you'll have to free() the array when you are done with it.
  1. int *array_without_doubles = remove_doubles( array_with_doubles );
  2. print_array( array_without_doubles );
  3. free( array_without_doubles );
Good luck.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 7:16 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC