Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Strcpy

Reply
Thread Tools

Strcpy

 
 
Rod Pemberton
Guest
Posts: n/a
 
      03-21-2006

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


 
Reply With Quote
 
 
 
 
Jake Thompson
Guest
Posts: n/a
 
      03-21-2006
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?

 
Reply With Quote
 
 
 
 
Richard Tobin
Guest
Posts: n/a
 
      03-21-2006
In article <(E-Mail Removed) .com>,
Jake Thompson <(E-Mail Removed)> wrote:

>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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-21-2006
"Jake Thompson" <(E-Mail Removed)> writes:
> 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 ****ed 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.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      03-21-2006
Jake Thompson wrote:

> 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
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      03-21-2006
Jake Thompson wrote:
> 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.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
 
Reply With Quote
 
Fred Kleinschmidt
Guest
Posts: n/a
 
      03-21-2006

"Jake Thompson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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.
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project



 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      03-21-2006
On 21 Mar 2006 13:59:53 -0800, "Jake Thompson"
<(E-Mail Removed)> wrote in comp.lang.c:

> 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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-21-2006
"Jake Thompson" <(E-Mail Removed)> writes:
> 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.)

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-21-2006
"Default User" <(E-Mail Removed)> writes:
> Jake Thompson wrote:
>
>> 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*


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

] Sorry for my outburst

FWIW.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
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
strcpy of a class object leonkatz@gmail.com C++ 11 01-20-2005 04:44 AM
strcpy and the copy constructor RonHiler C++ 8 10-19-2004 06:30 AM
C++ Compiler with a -Wwarn-use-of-strcpy or similar option?? Paul Sheer C++ 4 09-14-2004 08:38 PM
C++ Compiler with a -Wwarn-use-of-strcpy or similar option?? Paul Sheer C++ 7 09-10-2004 05:07 PM
strcpy Mike Mimic C++ 9 05-17-2004 08:12 PM



Advertisments