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

K

kaizen

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
 
G

Guest

kaizen said:
{ 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++
 
?

=?iso-8859-1?q?Stephan_Br=F6nnimann?=

kaizen said:
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
 
J

Jim Langston

kaizen said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top