Question

I'm trying to pass a 2d-array (array of strings) to a function in C, but am running into an "incompatible pointer type" warning.

In function ‘main’:
warning: passing argument 1 of ‘addCodonstoHash’ from incompatible pointer type

Code follows below. I am trying to create a hash table using uthash that will have 3-letter strings as keys (representing DNA codons), and associated char values representing the amino acid that the codon is translated to. So in essence, from the below code, I would want a hash table of the form

{"GCT" : 'A', "GCC" : 'A', "GCA" : 'A', "GCG" : 'A', "GCN" : 'A'}

where 'A' represents Alanine. But right now, I'm having trouble simply passing the array of codons to the function that should parse them and add them to the hash table. I've read through Incompatible pointer type and Passing an array of strings as parameter to a function in C to no avail... I thought I understood how arrays are allocated and degrade to pointers, but obviously not.

Furthermore, when I try to compute the length of the array in addCodonstoHash (there are going to be many differently-sized arrays representing the various amino acids), it's returning 8 and 2, respectively, from the printf debug lines, and I can't figure out why this is so. Can you guys steer me in the right direction?

/* Helper method for adding codons to hash table from initialized arrays */
void addCodonstoHash(char AAarray[][4], char AAname)
{   int i, size1, size;
    size1 = sizeof(AAarray);
    size  = size1 / sizeof(AAarray[0]);

    /* Debugging lines */
    printf("%d\n", size1);
    printf("%d\n\n",size);

    for (i = 0; i < (sizeof(AAarray) / sizeof(AAarray[0])); i++)
    {   add_codon(AAarray[i], AAname);
        printf(AAarray[i]);
        printf("\n");
    }
}


int main(int argc, char *argv[])
{   /*
     * Generate arrays for codons corresponding to each amino acid, will
     * all eventually be incorporated into hash table as keys, with AA
     * values.
     */
    const char Ala[][4] = {"GCT", "GCC", "GCA", "GCG", "GCN"};
    ...


    addCodonstoHash(Ala, 'A');

    delete_all(); 
    return 0;
}

Thanks!

Était-ce utile?

La solution

Ala has type const char(*)[4] (except when it is used as sizeof or & operand), but the parameter has type char(*)[4]. You need to declare this one as const.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top