Trouble with strcat

topic posted Fri, January 13, 2006 - 4:34 PM by  offlineWTL
Share/Save/Bookmark
Advertisement
I am writing a program in C but I am getting an error when it compiles that doesn't make any sense:
Warning: passing argument 1 of 'strcat' makes pointer from integer without a cast.

The relevant bits of code are:
The variables:
char da = 'A';
char db = 'C';
char dc = 'G';
char dd = 'T';
char dna1 [100000];

Where the error appears:
if(random == 1)
strcat(da, dna1);
else if (random == 2)
strcat(db, dna1);
else if (random == 3)
strcat(dc, dna1);
else
strcat(dd, dna1);

It doesn't like strcat for some reason. Both are chars, so it *should* work as far as I can tell.

Anyone have any suggestions to a c newbie?

Thanks!
posted by:
WTL
online WTL
Canada
Advertisement
Advertisement
  • Re: Trouble with strcat

    Fri, January 13, 2006 - 5:12 PM

    WTL,

    I believe strcat() takes char *'s as arguments, which is why it's complaining -- a char isn't a char *. Also, strcat expects the source strings to be null-terminated. The following example illustrates one usage:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    int main( int iArgc, char *pArgv[] )
    {
    char *cA = "A";
    char *cB = "B";

    char cDest[10] = {0};

    strcat( cDest, cA );
    strcat( cDest, cB );

    printf( "%s\n", cDest );

    return 1;
    }

    PugsleyButt:~/devstuff/c jmzorko$ gcc strcat.c -o strcat
    PugsleyButt:~/devstuff/c jmzorko$ ./strcat
    AB

    I hope this helps. BTW, the string handling in C++ is much better than that in C, but yeah, sometimes ya just got no choice!

    Regards,

    John

    Falling You - exploring the beauty of voice and sound
    www.fallingyou.com
  • Re: Trouble with strcat

    Fri, January 13, 2006 - 7:36 PM

    WTL,

    ... oh, and just to make sure, this is a really common error with beginning C and C++ developers. I remember running into the very same thing when I first learned C, waaaay back in 1986 on an Atari 1040ST (I believe it was the Metacompco C compiler).

    Anyway, what Jon says is also true -- sprintf can be your friend here as well.

    Regards,

    John, who interestingly enough is on a straight-C project currently (adding functionality to the VideoLAN client)

    Falling You - exploring the beauty of voice and sound
    www.fallingyou.com
  • Re: Trouble with strcat

    Wed, February 1, 2006 - 12:41 PM
    As a bit of a follow up, thanks for the pointers folks. I managed to beat the code with a stick long enough to get it to do what I wanted.

    And then a few days later (of course) , the client changed the requirement that it be done in C and asked for additional functionality, so I re-wrote it in Perl.

    But, I went out and picked up a newer C++ manual and see what I was doing wrong. Thanks again!
    • Re: Trouble with strcat

      Wed, February 1, 2006 - 5:50 PM

      WTL,

      If you're going to go C++, things get a lot better (once you get comfy with C++, it can be a lot different than C) -- streams are much safer than char arrays, and ostringstream is much safer than sprintf.

      Regards,

      John

      Falling You - exploring the beauty of voice and sound
      www.fallingyou.com
      • Caution - STRCPY Library is dangerous

        Thu, February 2, 2006 - 6:56 PM
        As a side-note many of the strcpy library functions are considered dangerous due to their naivete with respect to buffer overflow security attacks. These days, if you are programming anything serious, its important to a moment to understand them and use safe variations where appropriate.

        buildsecurityin.us-cert.gov/port...9.xml
        • Re: Caution - STRCPY Library is dangerous

          Thu, February 2, 2006 - 8:32 PM

          Jason,

          Right on -- that's what I was trying to say by stating that strings are safer when using C++-specific ways of dealing with them. If you can't, though -- use the strnX functions instead of the strX ones ... but really, just make your life easier and use C++ strings (std::string and friends).

          Regards,

          John

          Falling You - exploring the beauty of voice and sound
          www.fallingyou.com