error C2664: 'strcpy' : cannot convert parameter 2 from 'char' to 'const char *'

Discussion in 'C++' started by kaizen, Jan 20, 2006.

  1. kaizen

    kaizen Guest

    Hi,

    i wrote the code in C and compiled in VC++ compiler. at that time it
    has thrown the below mentioned error.

    error C2664: 'strcpy' : cannot convert parameter 2 from 'char' to
    'const char *'
    Conversion from integral type to pointer type requires
    reinterpret_cast, C-style cast or function-style cast

    Herewith i have added the code also.
    -----------------------------------
    #include "stdafx.h"

    int _tmain(int argc, _TCHAR* argv[])
    {
    char message[15]=":80a1:SSBKRO";
    char *location;
    char* receiver='\0';
    char* rec='\0';
    int i,j,cnt=0;
    int mt511=(int) strlen(":80a1:");
    printf("%s\n",message);
    if((location=strstr(message,":80a1:")) != NULL)
    {
    for(i=0;i<mt511;i++)
    location++;
    for(j=mt511;j<mt511+8;j++)
    while(*location!='\0')
    { strcpy(rec,message[j]);
    strcat(receiver,rec);
    cnt++;
    }
    /*for(i=0;i<mt511;i++)
    location++;
    strncpy(receiver,location,cnt);*/
    }
    else
    {
    for(i=mt511;i<cnt;i++)
    {
    strcpy(rec,"X");
    strcat(receiver,rec);
    }
    }
    printf("%s\n",receiver);
    return 0;
    }
    ----------------------------------------


    Kindly help me to resolve this issue. Thanks In advance.


    Regards,
    kaizen
    kaizen, Jan 20, 2006
    #1
    1. Advertising

  2. Re: error C2664: 'strcpy' : cannot convert parameter 2 from 'char'to 'const char *'

    kaizen wrote:

    > { strcpy(rec,message[j]);


    second parameter is a character, and should be a C-string (const char*)

    perhaps you wanted to do like:

    char tmp[2];
    tmp[0]=message[j]; tmp[1]='\0';
    strcat(receiver, tmp);

    but in general your coding style isnt too nice IMHO, also this looks
    more like C code not C++
    =?UTF-8?B?UmFmYcWCIE1haiBSYWYyNTY=?=, Jan 20, 2006
    #2
    1. Advertising

  3. kaizen wrote:
    > Hi,
    >
    > i wrote the code in C and compiled in VC++ compiler. at that time it
    > has thrown the below mentioned error.
    >
    > error C2664: 'strcpy' : cannot convert parameter 2 from 'char' to
    > 'const char *'
    > Conversion from integral type to pointer type requires
    > reinterpret_cast, C-style cast or function-style cast
    >
    > Herewith i have added the code also.
    > -----------------------------------
    > #include "stdafx.h"
    >
    > int _tmain(int argc, _TCHAR* argv[])
    > {
    > char message[15]=":80a1:SSBKRO";
    > char *location;
    > char* receiver='\0';
    > char* rec='\0';
    > int i,j,cnt=0;
    > int mt511=(int) strlen(":80a1:");
    > printf("%s\n",message);
    > if((location=strstr(message,":80a1:")) != NULL)
    > {
    > for(i=0;i<mt511;i++)
    > location++;
    > for(j=mt511;j<mt511+8;j++)
    > while(*location!='\0')
    > { strcpy(rec,message[j]);

    If you use &message[j] the complier error will disappear ...
    .... however I doubt that the program does what you'd like it to do.

    > strcat(receiver,rec);
    > cnt++;
    > }
    > /*for(i=0;i<mt511;i++)
    > location++;
    > strncpy(receiver,location,cnt);*/
    > }
    > else
    > {
    > for(i=mt511;i<cnt;i++)
    > {
    > strcpy(rec,"X");
    > strcat(receiver,rec);
    > }
    > }
    > printf("%s\n",receiver);
    > return 0;
    > }
    > ----------------------------------------
    >
    >
    > Kindly help me to resolve this issue. Thanks In advance.
    >
    >
    > Regards,
    > kaizen


    Stephan
    =?iso-8859-1?q?Stephan_Br=F6nnimann?=, Jan 20, 2006
    #3
  4. kaizen

    Jim Langston Guest

    "kaizen" <> wrote in message
    news:...
    > Hi,
    >
    > i wrote the code in C and compiled in VC++ compiler. at that time it
    > has thrown the below mentioned error.
    >
    > error C2664: 'strcpy' : cannot convert parameter 2 from 'char' to
    > 'const char *'
    > Conversion from integral type to pointer type requires
    > reinterpret_cast, C-style cast or function-style cast
    >
    > Herewith i have added the code also.
    > -----------------------------------
    > #include "stdafx.h"
    >
    > int _tmain(int argc, _TCHAR* argv[])
    > {
    > char message[15]=":80a1:SSBKRO";
    > char *location;
    > char* receiver='\0';


    The pointer receiver is pointing to one single character.

    > char* rec='\0';


    The pointer to rec is pointing to one single character

    > int i,j,cnt=0;
    > int mt511=(int) strlen(":80a1:");
    > printf("%s\n",message);
    > if((location=strstr(message,":80a1:")) != NULL)
    > {
    > for(i=0;i<mt511;i++)
    > location++;
    > for(j=mt511;j<mt511+8;j++)
    > while(*location!='\0')
    > { strcpy(rec,message[j]);


    strcpy is expecting 2 pointers. Where to copy to and where to copy from.
    The where to copy to should have enough space for the string it's going to
    copy from, and the string to copy from needs to be null terminated. Are you
    trying to add the one character at message[j] to receiver? If so, about
    where you declare rec make it:

    char rec[2] = " ";

    This makes a char array of 2 bytes, and initializes it to a space and a
    null. At this line simply do:

    rec[0] = message[j];

    This leaves the null terminator alone. So if message[j] contained 'x' rec
    would point to "x" (bytes of 'x' and the null terminator).

    Another problem, however, is that your receiver only has space for one byte.
    You need to make that big enough to hold your string, and null terminate it.
    Where you declare receiver make it something like:

    char receiver[100] = "";

    This will set aside 100 bytes for your string, and put the null terminator
    in the first byte so it is now a 0 length c-style string.

    Now the following will work, since strcat also wants two pointers to 2
    strings, both of them null terminated c-style strings.

    > strcat(receiver,rec);
    > cnt++;
    > }
    > /*for(i=0;i<mt511;i++)
    > location++;
    > strncpy(receiver,location,cnt);*/
    > }
    > else
    > {
    > for(i=mt511;i<cnt;i++)
    > {


    If you declare rec as above, char rec[2] and receiver as char receiver[100]
    the following will work without change.

    > strcpy(rec,"X");
    > strcat(receiver,rec);
    > }
    > }
    > printf("%s\n",receiver);
    > return 0;
    > }
    > ----------------------------------------
    >
    >
    > Kindly help me to resolve this issue. Thanks In advance.
    >
    >
    > Regards,
    > kaizen
    >


    Consider using std::string instead of c-style strings. With std::strings
    you could even do things like:

    std::string MyString;
    MyString = MyString + OtherString[j];

    without having to go through the null terminating pain.

    There are also other ways to do what you want above using c-style strings
    without null terminating, but std::strings are much easier.
    Jim Langston, Jan 21, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Harrison
    Replies:
    1
    Views:
    14,928
    Tim Mierzejewski
    Jun 25, 2003
  2. Suzanne
    Replies:
    0
    Views:
    998
    Suzanne
    Jun 25, 2003
  3. Jack Klein
    Replies:
    0
    Views:
    2,655
    Jack Klein
    Jun 25, 2003
  4. Alfonso Morra
    Replies:
    3
    Views:
    1,757
    Christopher Benson-Manica
    Aug 12, 2005
  5. John Smith
    Replies:
    6
    Views:
    5,294
    Mike Wahler
    Oct 31, 2005
Loading...

Share This Page