Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > error C2664: 'strcpy' : cannot convert parameter 2 from 'char' to 'const char *'

Reply
Thread Tools

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

 
 
kaizen
Guest
Posts: n/a
 
      01-20-2006
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

 
Reply With Quote
 
 
 
 
=?UTF-8?B?UmFmYcWCIE1haiBSYWYyNTY=?=
Guest
Posts: n/a
 
      01-20-2006
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++
 
Reply With Quote
 
 
 
 
=?iso-8859-1?q?Stephan_Br=F6nnimann?=
Guest
Posts: n/a
 
      01-20-2006
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

 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      01-21-2006

"kaizen" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
Error in program. cannot convert from 'char []' to 'char [10]' GRoll35 C++ 12 01-16-2006 09:22 PM
'atoi' : cannot convert parameter 1 from 'char' to 'const char *' John Smith C++ 6 10-31-2005 07:18 AM
cannot convert parameter 2 from 'const char *' to 'char *' Alfonso Morra C Programming 3 08-12-2005 01:41 PM



Advertisments