0

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!

Community
  • 1
  • 1
groundlar
  • 878
  • 1
  • 8
  • 17
  • Suggestion: you really should not be using C for this. Use a scripting language like Perl or Ruby for such tasks. C is not really suited to such tasks. It can do it, of course, but as you can see, it's a lot of headaches. A more scripting-oriented language will make everything way easier. – m0skit0 Sep 03 '12 at 12:01
  • This is covered in the [C FAQ](http://c-faq.com/ansi/constmismatch.html) - there is a problem with const and passing 2D (or greater) arrays to functions – Paul R Sep 03 '12 at 12:01
  • @m0skit0 I suppose I could write a script to generate the hash table, but this program is intended for massive data sets (say, 300GB files, so speed is crucial), so I thought C would be the way to go for this one. Otherwise I'm giving myself a headache for nothing haha – groundlar Sep 03 '12 at 12:13
  • You must consider if headaches are worth the (small?) speed increase. Perl has A LOT of modules for bioinformatics. And you will write code that works much faster. I suggest you give Perl a try and if speed doesn't convince you, you can try a mixed approach, which will include both worlds benefits (but also drawbacks...). Perl can easily use C libraries. – m0skit0 Sep 03 '12 at 14:30
  • Definitely looking into it, thanks for the ideas. I guess since this program will spend the majority of its time iterating over strings, I could just do that in C, and do the top-level file reading/writing in Perl... – groundlar Sep 04 '12 at 08:46

1 Answers1

1

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.

md5
  • 23,373
  • 3
  • 44
  • 93
  • So I've incorporated that, and while it no longer returns the warning, the printf lines are still printing unexpected things: sizeof(Ala) / sizeof(Ala[0]) is returning 2 from inside the addCodonstoHash function. However, when I do the same operation in the main function, it gives me the expected value of 5... What gives? – groundlar Sep 03 '12 at 12:18
  • In manly cases, an array is evaluated as a pointer to its first element. So, in the function `addCodonstoHash`, `AAarray` has type `const char(*)[4]`, and the result of the expression `sizeof AAarray / sizeof *AAarray` is equivalent to `sizeof(char *) / 4`. If a `char` has a sizeof of 8 bytes on your machine, it is egal to 2. To solve this problem, you have to pass an extra-parameter to indicate the size. – md5 Sep 03 '12 at 12:24
  • Thanks! I'll just add another parameter to addCodonstoHash for the size, then. Also, I can't believe this whole thing was just me forgetting to add the const keyword... Sorry guys, but thanks! – groundlar Sep 03 '12 at 12:26
  • @Kirilenko "if a char has a sizeof 8 bytes"? – Oktalist Sep 03 '12 at 12:44
  • Sorry, I meant "If a `char*` has a size of 8 bytes". But I can't edit this comment... – md5 Sep 03 '12 at 12:49