fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int in_array(int val,int* x,int x_size) {
  5. int i=0;
  6. while( i<x_size ) {
  7. if( *(x+i)==val ) return 1;
  8. ++i;
  9. }
  10. return 0;
  11. }
  12.  
  13. int main() {
  14. int x[] = {1,2,3,4,5,6,7,8,9,10};
  15. int A[] = {3,4,5,6};
  16. int B[] = {5,6,7,8};
  17.  
  18. int *tmp = (int *)malloc(sizeof(x));
  19. if(tmp){
  20. int size=0,i=0;
  21. for(i=0;i<sizeof(x)/sizeof(x[0]);++i) {
  22. if( in_array(x[i],A,sizeof(A)/sizeof(A[0])) ||
  23. in_array(x[i],B,sizeof(B)/sizeof(B[0])) ){
  24. ++size;
  25. *(tmp+i)=i;
  26. }else{
  27. *(tmp+i)=-1;
  28. }
  29. }
  30.  
  31. int *array = (int *)malloc(size*sizeof(int));
  32. if( array ){
  33. int *pos=array;
  34. for(i=0;i<sizeof(x)/sizeof(x[0]);++i) {
  35. if( 0<=*(tmp+i) ){
  36. *pos=x[*(tmp+i)];
  37. pos++;
  38. }
  39. }
  40. printf("[ ");
  41. for(i=0;i<size;++i) printf("%d ", *(array+i));
  42. printf("]\n");
  43. free(array);
  44. }
  45. free(tmp);
  46. }
  47. return 0;
  48. }
  49.  
  50.  
Success #stdin #stdout 0s 9424KB
stdin
Standard input is empty
stdout
[ 3 4 5 6 7 8 ]