Strcpy

J

Jake Thompson

I need to copy a value into a char * field.

I am currently doing this

strcpy(cm8link.type[count],"13");

but I get an error of

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

this used to when it was just cm8link.type but it errors when I make it


cm8link.type[count]

first of all why?

secondly how can i get the value into the field for this array
occurrence?

Thanks

Jake
 
V

Vladimir S. Oka

Jake Thompson opined:
I need to copy a value into a char * field.

I am currently doing this

strcpy(cm8link.type[count],"13");

You don't give its declaration, but I bet the above is of type `char`
rather than `char *`.
but I get an error of

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

this used to when it was just cm8link.type but it errors when I make
it


cm8link.type[count]

first of all why?

secondly how can i get the value into the field for this array
occurrence?

Please post a small compilable example of your code that exhibits the
problem (and it's output).


--
BR, Vladimir

Americans' greatest fear is that America will turn out
to have been a phenomenon, not a civilization.
-- Shirley Hazzard, "Transit of Venus"
 
D

Default User

Jake said:
I need to copy a value into a char * field.

I am currently doing this

strcpy(cm8link.type[count],"13");

but I get an error of

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

this used to when it was just cm8link.type but it errors when I make
it


cm8link.type[count]

first of all why?

We went over this last time. Post a COMPLETE, minimal program that
compiles and demonstrates the problem.

How can we possibly say anything without seeing the declarations of the
variables?
secondly how can i get the value into the field for this array
occurrence?

What field? What array? Where's my damn crystal ball?

You don't make easy for us to help you.



Brian
 
J

Jake Thompson

First of all I appreciate the help and certainly there is no need to
lash out

Secondly I did say that the field cm8link.type[count] is a char * field

I don't see the need to have the entire program listed. It is a one
line statement that obviously I am using the wrong way.

In a nutshell I need to move a literal to a char * that is part of an
array

currently as an example I am doing the following just so you know count
is normally incremented in a loop but here i set it to clearly define
that I want to move the value 13 into

cm8link.type[2],

int count = 2;

strcpy(cm8link.type[count],"13");

resulting in the error

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

'char *'
 
S

santosh

Jake said:
First of all I appreciate the help and certainly there is no need to
lash out

Nobody is lashing out at you.
Secondly I did say that the field cm8link.type[count] is a char * field

I don't see the need to have the entire program listed. It is a one
line statement that obviously I am using the wrong way.

But to see how and why you're using it the wrong way, we need to see
the sorrounding relevent source, don't we?
 
D

Default User

Jake Thompson wrote:

I don't see the need to have the entire program listed. It is a one
line statement that obviously I am using the wrong way.


Then figure it out yourself.



Brian
 
E

Eric Sosman

Jake Thompson wrote On 03/21/06 16:04,:
First of all I appreciate the help and certainly there is no need to
lash out

Secondly I did say that the field cm8link.type[count] is a char * field

... but since the compiler contradicted you (it
described the field as `const char'), didn't it occur
to you to offer the actual code so someone could try to
judge whether you or the compiler is in error?
I don't see the need to have the entire program listed. It is a one
line statement that obviously I am using the wrong way.

... and the "what's wrong with it" is clearly a
matter of context. Can you tell, without looking at
any of the rest of the program, whether the one line

x = 42;

is right or wrong? (If you think you can, you need to
re-open your C textbook.)
In a nutshell I need to move a literal to a char * that is part of an
array

currently as an example I am doing the following just so you know count
is normally incremented in a loop but here i set it to clearly define
that I want to move the value 13 into

cm8link.type[2],

int count = 2;

strcpy(cm8link.type[count],"13");

resulting in the error

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

'char *'

You claim that `cm8link.type[2]' is a `char*', while the
compiler claims it's a `const char'. Without further evidence
it would be injudicious to say which of you is right -- but
I'm forced to disclose, right here on my jury screening form,
that I am more likely to believe the compiler's testimony than
yours. Since I am obviously prejudiced, I won't be empanelled
(a lucky break for you; I'm in a mood to hang uncooperative
witnesses).
 
J

Jake Thompson

Well if it means dealing with a Dick as the alternative then hell yeah
I will figure it out myself.

Thank God your attitude is the minority here.

I don't know who pissed in your breakfast but dude lighten up
 
S

santosh

Jake said:
Well if it means dealing with a Dick as the alternative then hell yeah
I will figure it out myself.

Thank God your attitude is the minority here.

I don't know who pissed in your breakfast but dude lighten up

This is not a forum to throw mud at each other. Admittedly Default User
was rather curt in his reply, but the point he, I and several others
have made to you still stands, i.e. post the smallest compilable code
that exhibits your problem or failing that atleast post relevant
sarrounding code like data declarations, prototypes etc., so that we
don't have to keep guessing.

Also please quote the post to which you're replying. Most regulars in
this group, access it via nntp servers which might delete articles
after a certain time, unlike Google Groups. Including context makes the
post comprehendable in such cases.
 
K

Kenneth Brody

Jake said:
First of all I appreciate the help and certainly there is no need to
lash out

Secondly I did say that the field cm8link.type[count] is a char * field

Obviously it's not, as the error says it's a "const char".
I don't see the need to have the entire program listed. It is a one
line statement that obviously I am using the wrong way.

The problem is that without seeing the actual definition of
cm8link.type[count], people can only guess as to the "real"
problem. The compiler says one thing, and you say something
else. We tend to believe the error message over your claim
until you show us the compiler is wrong.
In a nutshell I need to move a literal to a char * that is part of an
array

An array of what?
currently as an example I am doing the following just so you know count
is normally incremented in a loop but here i set it to clearly define
that I want to move the value 13 into

cm8link.type[2],

But, what type is cm8link.type[2]? What is cm8link.type?
int count = 2;

strcpy(cm8link.type[count],"13");

resulting in the error

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

While tells us that cm8link.type[count] is of type "const char", and
not the "char *" that you continue to claim.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 
R

Rod Pemberton

Jake Thompson said:
Well if it means dealing with a Dick as the alternative then hell yeah
I will figure it out myself.

Thank God your attitude is the minority here.

I don't know who pissed in your breakfast but dude lighten up

Unfortunately, his attitude is the majority here. Certain individuals will
call you off-topic or a troll despite the fact that they've only posted
complaints and have never posted anything C related:

Brian "Default User"
CBFalconer
Richard Bos
etc...


Rod Pemberton
 
J

Jake Thompson

Sorry for my outburst


This is the function that I am calling



char *folderid;
struct cm8linkstruc cm8link; <----------------structure tag set to
cm8link



struct cm8linkstruc
{
char* type; /* type of item*/
<------------------------------------------field that I want to copy
the "13" too
char* desc; /* description of item */
char* item_increment; /*increment value for item
in folder */
char* itemid; /* id of returned item */
};


These are the lines of code that I am trying to execute in order to
copy the values too.

strcpy(cm8link.type[count],"13"); //Copy the number 13 to indicate
folder
strcpy(cm8link.desc[count],"Document "); //copy the description
strcpy(cm8link.desc[count],snumD); //copy the current doc counter to
the description
strcpy(cm8link.item_increment[count],snumD); //copy Document counter
cm8link.itemid[count] = ((DKPidICM*)part->getPidObject())->getItemId()
; //Get the itemid

Is this enough information to go off of?
 
R

Richard Tobin

Jake Thompson said:
Secondly I did say that the field cm8link.type[count] is a char * field

I don't see the need to have the entire program listed. It is a one
line statement that obviously I am using the wrong way.

Since you have this problem, it's clear that there's something
about the program that you don't understand. So we can't (and you
shouldn't) trust your judgement as to what is "obvious".

In particular, we can't trust your claim that cm8link.type[count]
is of type char *. Show us the whole program!

-- Richard
 
K

Keith Thompson

Jake Thompson said:
Well if it means dealing with a Dick as the alternative then hell yeah
I will figure it out myself.

Thank God your attitude is the minority here.

I don't know who pissed in your breakfast but dude lighten up

You need to provide context when you post a followup. Read
<http://cfaj.freeshell.org/google/>, and you'll understand how and
why.

Upthread, you wrote:

] I don't see the need to have the entire program listed. It is a one
] line statement that obviously I am using the wrong way.

If you already knew what the problem is, then presumably you wouldn't
need to ask for our help.

Conversely, the fact that you *are* asking for our help implies that
you really don't know what the problem is. If we offer you advice on
what information *we* need to help you solve *your* problem, then you
really should pay attention. We're not making you jump through hoops
just for the fun of it; we're asking you to help us to help you. If
you're unwilling to do so, then we can't help you.
 
D

Default User

Jake said:
Well if it means dealing with a Dick as the alternative then hell yeah
I will figure it out myself.

Thank God your attitude is the minority here.

As you can't be bothered to follow simple instructions when you're the
one wanting help, plus you refuse to quote any context, it's pretty
obvious what happens next.


*plonk*



Brian
 
F

Flash Gordon

Jake said:
First of all I appreciate the help and certainly there is no need to
lash out

It tends to get frustrating when people ignore the advice to post enough
information to allow them to be helped.
Secondly I did say that the field cm8link.type[count] is a char * field

I don't see the need to have the entire program listed. It is a one
line statement that obviously I am using the wrong way.

Since you don't know what is wrong, how do you know it isn't something
else causing the problem?

Man goes to mechanic, "my car won't start, here's the start motor, whats
the problem?"
Mechanic, "how do I know without the entire car?"

Man tries the same else where with the same result.

Swearing, man goes home and puts starter motor back in car. Man's wife
comes out and says, "what are you up to? Oh, and by the way, the car ran
out of petrol and I got some friends to help push it back here."
In a nutshell I need to move a literal to a char * that is part of an
array

currently as an example I am doing the following just so you know count
is normally incremented in a loop but here i set it to clearly define
that I want to move the value 13 into

cm8link.type[2],

int count = 2;

strcpy(cm8link.type[count],"13");

resulting in the error

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

'char *'

That's easy. Either the error refers to a different line of
cm8link.type[count] is of type const char * despite what you claim. Of
course, there is the remote possibility that the compiler is lying, but
you being wrong about the source of the problem is *far* more likely.

In future post a *complete* program exhibiting the problem or it is
highly unlikely that anyone will bother to even try and help you.
 
F

Fred Kleinschmidt

Jake Thompson said:
First of all I appreciate the help and certainly there is no need to
lash out

Secondly I did say that the field cm8link.type[count] is a char * field

I don't see the need to have the entire program listed. It is a one
line statement that obviously I am using the wrong way.

In a nutshell I need to move a literal to a char * that is part of an
array

currently as an example I am doing the following just so you know count
is normally incremented in a loop but here i set it to clearly define
that I want to move the value 13 into

cm8link.type[2],

int count = 2;

strcpy(cm8link.type[count],"13");

resulting in the error

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

'char *'

But you were probably wrong when you said "cm8link.type[count] is a char *
field".
I'll bet that cm8link_type is a char * field, not cm8link.type[count]. In
other words,
somewhere you have:

char *cm8link.type;
of
char cm8link.type[n]; /* where n is some number */

If you want to place the characters "13" into cm8link.type beginning at
position "count", then you want:
strcpy (&cm8link.type[count], "13" );
Hopefully cm8link.type is of length at least (count+3) or you will overwrite
memory.
 
J

Jack Klein

Sorry for my outburst

OK, but I'm still not sure that you're getting the point.
This is the function that I am calling

You aren't showing a function at all.
char *folderid;
struct cm8linkstruc cm8link; <----------------structure tag set to
cm8link



struct cm8linkstruc
{
char* type; /* type of item*/
<------------------------------------------field that I want to copy
the "13" too
char* desc; /* description of item */
char* item_increment; /*increment value for item
in folder */
char* itemid; /* id of returned item */
};

At last, a definition of the structure! This structure contains four
members (there is no such thing as a "field" defined by the C
language), and each of the fields is a pointer to char.

Up above you show the definition of an object of this type, named
"cm8link". Since you're still not posting the real code that your
compiler is seeing, there is information lacking.

Is "cm8link" defined at file scope (outside of all functions), or is
it defined at local scope (inside of a function)? It makes a
difference, should your code ever compile, because you are heading for
a run time problem.

If "cm8link" is defined at file scope, the four char pointers are
initialized to NULL. If it is defined in a local scope, the four char
pointers are not initialized at all. In either case, they do not
point to valid memory that you can read from or write to.
These are the lines of code that I am trying to execute in order to
copy the values too.

strcpy(cm8link.type[count],"13"); //Copy the number 13 to indicate
folder

I know you resent being asked for enough information to understand
what mistakes you are making, BUT WHAT THE HELL IS "count"?!? WHERE
IS "count" DEFINED?!?
strcpy(cm8link.desc[count],"Document "); //copy the description
strcpy(cm8link.desc[count],snumD); //copy the current doc counter to
the description

WHAT THE HELL IS "snumD"?!?
strcpy(cm8link.item_increment[count],snumD); //copy Document counter
cm8link.itemid[count] = ((DKPidICM*)part->getPidObject())->getItemId()
; //Get the itemid

Is this enough information to go off of?

No, actually, it is not. If you have actually properly initialized
the character pointers to valid memory that you have the right to
write to, then cm8link.desc[count] is a SINGLE CHARACTER, and you
can't copy a string into a SINGLE CHARACTER. If you haven't
initialized the character pointers, they don't point anywhere and you
can't write to them at all.

Multiple people have tried to explain to you, most of them patiently,
but you aren't getting it.

POST THE ACTUAL CODE THAT YOU ARE COMPILING. OF THE WHOLE FUNCTION.
COPY IT FROM YOUR TEXT EDITOR AND PASTE IT INTO A MESSAGE. ALSO COPY
THE DECLATATION OF EACH DATA TYPE, AND THE DEFINITION OF EACH OBJECT
THAT IS MENTIONED IN THE CODE. PASTE IT ALL INTO YOUR MESSAGE.

There are several possible different mistakes that you might be
making, and nobody here is willing to put that much effort into
guessing, maybe correctly or maybe incorrectly.

STOP TRYING TO GUESS HOW LITTLE REAL INFORMATION PEOPLE NEED TO HELP
YOU. YOU'RE GUESSING WRONG. IF YOU AREN'T WILLING TO PROVIDE
EVERYTHING I ASKED FOR ABOVE, THEN YOU SHOULD GO AWAY AND FIGURE IT
OUT FOR YOURSELF.

Now I've got a sore throat from ALL THE SHOUTING.
 
K

Keith Thompson

Jake Thompson said:
Sorry for my outburst

A refreshing response, thank you. Everyone has bad moments every now
and then.
This is the function that I am calling



char *folderid;
struct cm8linkstruc cm8link; <----------------structure tag set to
cm8link



struct cm8linkstruc
{
char* type; /* type of item*/
<------------------------------------------field that I want to copy
the "13" too
char* desc; /* description of item */
char* item_increment; /*increment value for item
in folder */
char* itemid; /* id of returned item */
};


These are the lines of code that I am trying to execute in order to
copy the values too.

strcpy(cm8link.type[count],"13"); //Copy the number 13 to indicate
folder
strcpy(cm8link.desc[count],"Document "); //copy the description
strcpy(cm8link.desc[count],snumD); //copy the current doc counter to
the description
strcpy(cm8link.item_increment[count],snumD); //copy Document counter
cm8link.itemid[count] = ((DKPidICM*)part->getPidObject())->getItemId()
; //Get the itemid

Is this enough information to go off of?

It's a good start.

Consider the call

strcpy(cm8link.type[count], "13");

I'll assume count is an integer object.

cm8link is of type struct cm8linkstruc.
cm8link.type is of type char*.
cm8link.type[count] is of type char
The first argument to strcpy() is a char*, not a char.

That's what you're doing wrong. What you should do is a trickier
question.

What is count? What does its value indicate? A count of what?

Since cm8link.type is a char*, it's reasonable to have it point to
(the first character of) the string "13". The simplest way to do this
is by an assignment:

cm8link.type = "13";

cm8link.type will then point to the first character of a string
literal. Allocation is taken care of for you, but you can't modify
the contents of the string.

(It might make more sense for cm8link.type to be an int, and just
assign the value 13 to it -- or better yet, use some symbolic name
like FOLDER, which could be a macro or an enum constant. But that's a
design issue, not a correctness issue.)

For a more general solution, you can either make cm8link.type point to
an existing declared object (make sure the object doesn't cease to
exist before you're done with it), or use malloc() to allocate space.
For example:

char *type = "13";
...
cm8link.type = malloc(strlen(type) + 1);
/* check whether malloc() succeeded */
strcpy(cm8link.type, type);

But it's still hard to tell just what you're trying to do. Your use
of "count" seems to imply that you want an array of something. Do you
want an array of struct cm8linkstruc objects? If so, you can either
declare an array (if you know how many you want), or you can declare a
*pointer* to a struct cm8linkstruc, and initialize it to point to an
array by calling malloc().

For example (this is a rough outline, not compiled or tested):

struct cm8linkstruc *arr_ptr;
arr_ptr = malloc(sizeof *arr_ptr * how_many);
/* check whether malloc() succeeded */
arr_ptr[count].type = "whatever";

The code fragment you posted is certainly an improvement over what
you've shown us previously, but it's still not valid C, and it's still
incomplete. We still can't really tell how it fits into any larger
context.

I suspect the underlying problem is that you're writing code too
early. You need to come up with a consistent design first, and then
express it in C code. (With enough experience, you can often write
the design directly in C, but frankly I don't think you're there yet.)
 
K

Keith Thompson

Default User said:
As you can't be bothered to follow simple instructions when you're the
one wanting help, plus you refuse to quote any context, it's pretty
obvious what happens next.


*plonk*

Then you probably missed his followup, in which he wrote:

] Sorry for my outburst

FWIW.
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top