1

I´m new in C programming and have a problem I cant solve.
From a file with a lot of information, structured like
"id" \t 2048 times '0' or '1' e.g.:

81283 \t   0101011010.....0110

I've to find, from a new sequence introduced by the user, the 3 most similar elements with the same size of the sequence introduced by the user.

E.g. sequence introduced by user, 010101, the program has to show like

  • n1= 010101
  • n2= 010100
  • n3= 010010

Those are the 3 most similar sequences from all the 2048 '0' '1' in each line.

I hope that the problem was well explain. I have this code below, but it does not solve the problem.

Can anyone help me?

Thanks.

int procura(int *v, int *c, int tam){
int i,j,t;
FILE *fp;
fp=fopen("fich.txt","r");
v=(int*)malloc(sizeof(int));
c=(int*)malloc(sizeof(int));
while(!feof(fp)){
    fscanf(fp,"%d",&v);
}
t = sizeof(v)/sizeof(*v);
for(i=0;i<tam;i++){
    for(j=0;j<t;j++){
        if(c[i]==v[j])
            return 1;
        else
            return 0;
    }
}

}

int main(){
int *v, *c;
int i,tam,a;
printf("array size\n");
scanf("%d",&tam);
c=(int*)malloc(tam*sizeof(int));
printf("Seq\n");
for(i=0;i<tam;i++){
    scanf("%d",&c[i]);
}
printf("%d",procura(v,c, tam));

return 0;

}

Cardoso
  • 11
  • 3
  • Can you make that compilable warning-free? Take the [tour] and check [mcve]. – Yunnosch Jun 05 '17 at 17:57
  • Yes, i change a few things, and it compiles with out errors – Cardoso Jun 05 '17 at 18:03
  • Can you provide code which compiles without strict warnings? Use the things mentioned in the answer by Some programmer dude. – Yunnosch Jun 05 '17 at 18:13
  • What is your specific problem? Looping over all the bits in the long sequence? Comparing from one starting position? Storing the three lowest non-similarities with their starting position? Comment your code to show where you plan to do these things (or already did them). Do so in English to have best chances for help here. – Yunnosch Jun 05 '17 at 19:06
  • Why did you undo the changes by dbush? Indentation and correct markdown is very helpful, that is what dbush did for you. Consider rolling back your last edit. While you do that, try to get your posted code compilable without strict warnings. – Yunnosch Jun 05 '17 at 19:11

1 Answers1

3

A few things:

  • v=(int*)malloc(sizeof(int));
    c=(int*)malloc(sizeof(int));
    

    These overwrites the pointers, so you lose the original v and c.

  • sizeof(v)/sizeof is a syntax error

  • Getting the size of a pointer with sizeof returns the size of the pointer, not what it points to

  • Your reassignment of v and c above makes those variables points to memory for only a single int, and then you loop over tam elements meaning you go out of bounds of the memory

  • With fscanf(fp,"%d",&v) you pass a pointer to the pointer

  • Read Why is “while ( !feof (file) )” always wrong?

  • No error checking, what happens if you can't open a file?

  • Your function will return immediately after the first c[i]==v[j] check, and it will return unconditionally

And probably much more.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621