tags:

views:

81

answers:

3

Good day all, I have two array which I want to copy some data from the first array to the second array without repeating any values in the second array.Could someone tell me what I am not getting here.

#include <stdio.h>          
#include <stdlib.h>         
#include <string.h>



 main()
 {
 char *hours;
 char *strl;
 hours={0,0,0,0,1,1,2,2,2,5,5,10,10,10,10,10,.,.,.,23}

 strl=calloc(100,sizeof(char));

 sprintf(strl, "%d", hours);

 if(strcmp(strl,hours))
 {

  if(*strl)
 strcpy(strl,hours);

 } 
printf("%s ",strl;
}
A: 

First of all, you have two arrays of chars. Now, in C, arrays of chars are often used to store strings, but you are not doing that. These are not strings at all, so forget all about the string functions in the library.

What you actually have are two arrays of tiny integers, so you handle them as you would an int array.

#include <stdio.h>           
#include <stdlib.h>          

 int main() 
 { 
   char *hours; 
   char *strl; 
   int i;
   int j;
   char last;
   /* I'm going to assume the the values in hours are sorted. */
   hours={0,0,0,0,1,1,2,2,2,5,5,10,10,10,10,10,.,.,.,23} 

   strl=calloc(100,sizeof(char)); 

   j = 0;
   last = hours[0];
   for(i=1; i < 100; ++i) // I'm going to assume hours has 100 items
   {
       if (hours[i] != last)
       {
           strl[j++] = last;
           last = hours[i];
        }
   }
   strl[j++] = last;    // UPDATE (Thanks, Matthew)
 }

/*    printf("%s ",strl; 
    You'll need a different way of printing you results */
} 
James Curran
-1, sure he'd have an array of `char` if the init syntax where correct. Small enough `int` are directly converted to `char` without problem.
Jens Gustedt
@Jens, James is correct. You can *not* use the string functions without a NUL-terminated array. His array ends with 23, not NUL.
Matthew Flaschen
You have a off-by-one error. The final unique value (23 here) of `hours` will not get copied.
Matthew Flaschen
@Jens: I didn't say he had an array of `int`. I said he had an array of "tiny integers" AKA `char`s. I'm explaining how *he* must think about it; not how the compiler thinks of it.
James Curran
I didn't say that he may use the string functions. In C, `char` is an integer type, so what he was intending would lead to a legal `char` array, so he could do any legal integer arithmetic, comparison and whatever on it.
Jens Gustedt
@Jens: and that's what *I'M* saying.....
James Curran
A: 

You probably want to do something more like:

char *dst = strl;
char *cur = hours;
char prev = '\0';
while(*cur) {
  if(*cur != last) {
    prev = *dst = *cur;
    dst++;
  }
  cur++;
}

Oh yea and as noted by others, your arrays aren't terminated with '\0', so standard string functions don't work (And for that matter neither will my loop).

Second none of the standard C library functions do what you want... I'll leave it up to you to fix my loop. (Note it only works if the array is sorted)

Spudd86
You are assuming a NUL terminated string -- actually, here the zeros are at the start...
James Curran
A: 

As James said, you can't use string functions because your arrays aren't NUL-terminated. Even if you could, strcmp would always return non-zero (specifically negative), and *strl would always be 0, which means the inner if always fails. Finally strcpy does not eliminate any duplicates. If your array is sorted, you can do this by checking for duplicate consecutive values. Otherwise, you can use a hash set/hash table like GHashTable.

Matthew Flaschen
Thank you all,you guys have really being helpful.I have really spent a handful of time on this and not knowing, I was really of the mark.
chriscol