Domanda

I've been working on this for a while (in C) and can't figure it out. I have a buffer containing an array of chars. I've used qsort to sort through the array and it's all in proper order now. I now need to remove duplicates (or just print out the list without duplicates). There's a caveat: the chars are grouped into groups of N chars (the N given by the user). So it's not just comparing one char next to the other; it's comparing groups of them against each other.

So for example: if the input is AADDBBEECCEE and the N given by the user is 2, the result would be AABBCCDDEE (with one of the EE's removed).

I know I have to use memcmp, but I'm confused about the syntax. I'm trying:

i=0;
int result;
int k;
while(i<bufferSize-nValue){
    result = memcmp(buffer[i], buffer[i+nValue], nValue);
    if(result==0){
       i=i+nValue;
    }
    else{
       for(k=0; k<nValue; k++){
          printf("%c",buffer[i]);
          i++;
        }
     }
 }

where buffer is the array, nValue is N, bufferSize is total number of elements in array. I keep getting segmentation fault when running the code.

Thanks for your help, everyone!

È stato utile?

Soluzione

You wrote:

memcmp(buffer[i], buffer[i+nValue], nValue);

memcmp() takes pointers. You probably mean buffer+i and buffer+i+nValue for the arguments. If that's the answer, I'm surprised your compiler didn't warn about that. Did you activate warnings?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top