473,233 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,233 software developers and data experts.

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

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
3 16674
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: John Smith | last post by:
What's wrong with the use of atoi in the following code? Why do I get the error message: 'atoi' : cannot convert parameter 1 from 'char' to 'const char *' char cBuffer; void...
1
by: DukeRock | last post by:
This is the function I'm getting the error on: S32 GuiAviBitmapCtrl::fileOpen() { S32 rval; if (!dStrcmp(mAviFilename,"")) return MOVERR_NOVIDEOSTREAM; rval =...
1
by: stillh2os | last post by:
Hello. I'm new to .NET, and I'm trying to implement a callback function. I want my managed C++ code to call an unmanaged function, passing in a callback function that the unmanaged C/C++ code...
1
by: asenthil | last post by:
Hai this is senthil... I had tried to write a string which fetched from a database. into a file... when i tried to compile the solution the following error occurs like this error C2664:...
8
by: =?Utf-8?B?V2hpdG5leSBLZXc=?= | last post by:
Hi there, I'm having a bit of trouble using an HRASCONN object as a class member variable inside my managed C++ class. When I call RasDial() and pass in the address of my HRASCONN object, I get...
1
by: td0g03 | last post by:
I have no idea why am I getting an error C2664: 'parseInput' : cannot convert parameter 2 from 'main::WORD_STRUCT ' to 'struct WORD_STRUCT ' #include <stdio.h> #include <stdlib.h> #include...
2
by: munjao1 | last post by:
i am passing an char ** argument and returning its value as int i am getting an error in doing that as cannot convert int to const char* what can i do?
1
codexparse
by: codexparse | last post by:
I am writing a browser program in C++ Builder 6 that loads a web page using the following code: void __fastcall TForm1::ToolButton1Click(TObject *Sender) { wchar_t buff; ...
5
by: slizorn | last post by:
well the error i get is the title above: error C2664: 'searchTree' : cannot convert parameter 2 from 'const char *' to 'char' error is form this line searchTree(treeObj->root ,data1.c_str());...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.