Question

I have this structure which i am trying to initialize using the following code. It gets run time error when trying to initialize "finger_print"

What is wrong with it?

typedef struct fpinfo
{
    unsigned long chunk_offset;
    unsigned long chunk_length;
    unsigned char fing_print[33];

}fpinfo;

the function:

struct fpinfo* InitHTable(struct fpinfo ht[][B_ENTRIES])
{
    unsigned char garb[33]={0};

        for (int j = 0; j < BUCKETS; ++j) 
        {
            for (int k = 0; k < B_ENTRIES; ++k)
            {
                ht[j][k].chunk_offset=0;
                ht[j][k].chunk_length=0;
                strcpy((char*)ht[j][k].fing_print[32],(const char*)garb);
                //ht[j][k].fing_print[32]=0;
            }
        }
        curr_tanker=1;
        return &ht[0][0];
}

It is the same with strncpy()

Was it helpful?

Solution

strcpy((char*)ht[j][k].fing_print[32],(const char*)garb);

You're treating the last character of fing_print as a pointer and attempting to write to it. Perhaps you meant:

strcpy((char*)ht[j][k].fing_print,(const char*)garb);

OTHER TIPS

Use strncpy if you want to specify number of characters that should be copied. Replace these lines:

strcpy((char*)ht[j][k].fing_print[32],(const char*)garb);
//ht[j][k].fing_print[32]=0;

with these:

strncpy((char*)ht[j][k].fing_print, (const char*)garb, 32);
ht[j][k].fing_print[32] = '\0';

The statement strcpy((char*)ht[j][k].fing_print[32],(const char*)garb); doesn't make sense. It takes element 32 of the fing_print array, interprets that as an address, and tries to write the contents of garb to that address (which should have no side effect at all, because garb only contains zeros, and is regarded as a 0-length string by strcpy()).

If you want to set ht[j][k].fing_print to all zeros, use memset(ht[j][k].fing_print, 0, sizeof(ht[j][k].fing_print)).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top