By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,876 Members | 1,557 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,876 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Jan 20 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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++
Jan 20 '06 #2

P: n/a
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

Jan 20 '06 #3

P: n/a

"kaizen" <ks*****@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr 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.
Jan 21 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.