Thread: don't see a bug

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    222

    don't see a bug

    hi,


    well this is the code:

    Code:
     char **bowtTable = emalloc(sizeof(char*)*(len+1));
    long u;
    char line[BUFSIZ];
    while ( fgets(line, sizeof line, file) != NULL ){
    	if ( sscanf(line, "%*s  %*s  %*s %ld %5000s %*s %ld", &start,readstr,&cpys) == 3 ){
    	  llen = strlen(line);
    		
    	long z = start;
    	bowtTable[z] = (char *)emalloc(sizeof(char)*(llen+1));
    	bowtTable[z] = line;
    						
    	printf("> %ld %ld -> %s\n",z,startt,bowtTable[u]);
    	printf("line len: %d %s\n",llen,bowtTable[z]);
    	u=z;
    }
    }
    so everytime i encounter the line i store it into my bowtTable under the index z.so i get something like this as a printout

    bowtTable[5] = aaaaaaaaaaaaaaaaaaaaa
    bowtTable[7] = bbbbbbbbbbbbbbbbbbbbb

    but now when i chech bowtTable[5] (in the same iteration of while loop) i see that
    bowtTable[5] = bbbbbbbbbbbbbbbbbbbbb instead of aaaaaaaaaaaaaaaaaaa. This happens as soon as i set bowtTable[7] = bbbbbbbbbbbbbbbbbbbbbb.

    how can this be what am i doing wrong it is like i am somehow referencing every previous entry to my next. so the thole table then, in the end has the last read line under every possible index in bowtTable. I know it is something simple but i cannot see my mistake

    can somebody take a look

    thank you

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    	bowtTable[z] = line;
    needs to be
    Code:
    	strcpy(bowtTable[z],line);
    But then I don't have an idea what emalloc() is

    Kurt

Popular pages Recent additions subscribe to a feed