Search code examples
cinitializationruntime-errorstrcpystrncpy

strcpy() creates error


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()


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);