Strcpy

Discussion in 'C Programming' started by Jake Thompson, Mar 21, 2006.

  1. 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
    Jake Thompson, Mar 21, 2006
    #1
    1. Advertising

  2. 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"
    Vladimir S. Oka, Mar 21, 2006
    #2
    1. Advertising

  3. Jake Thompson

    Default User Guest

    Jake Thompson wrote:

    > 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
    Default User, Mar 21, 2006
    #3
  4. 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 *'
    Jake Thompson, Mar 21, 2006
    #4
  5. Jake Thompson

    santosh Guest

    Jake Thompson wrote:
    > 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?
    santosh, Mar 21, 2006
    #5
  6. Jake Thompson

    Default User Guest

    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
    Default User, Mar 21, 2006
    #6
  7. Jake Thompson

    Eric Sosman Guest

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

    --
    Eric Sosman, Mar 21, 2006
    #7
  8. 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
    Jake Thompson, Mar 21, 2006
    #8
  9. Jake Thompson

    santosh Guest

    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.
    >
    > 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.
    santosh, Mar 21, 2006
    #9
  10. Jake Thompson wrote:
    >
    > 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:>
    Kenneth Brody, Mar 21, 2006
    #10
  11. "Jake Thompson" <> wrote in message
    news:...
    > 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
    Rod Pemberton, Mar 21, 2006
    #11
  12. 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?
    Jake Thompson, Mar 21, 2006
    #12
  13. In article <>,
    Jake Thompson <> 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
    Richard Tobin, Mar 21, 2006
    #13
  14. "Jake Thompson" <> 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 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.

    --
    Keith Thompson (The_Other_Keith) <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.
    Keith Thompson, Mar 21, 2006
    #14
  15. Jake Thompson

    Default User Guest

    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
    Default User, Mar 21, 2006
    #15
  16. Jake Thompson

    Flash Gordon Guest

    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
    Flash Gordon, Mar 21, 2006
    #16
  17. "Jake Thompson" <> wrote in message
    news:...
    > 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
    Fred Kleinschmidt, Mar 21, 2006
    #17
  18. Jake Thompson

    Jack Klein Guest

    On 21 Mar 2006 13:59:53 -0800, "Jake Thompson"
    <> 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/~ajo/docs/FAQ-acllc.html
    Jack Klein, Mar 21, 2006
    #18
  19. "Jake Thompson" <> 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) <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.
    Keith Thompson, Mar 21, 2006
    #19
  20. "Default User" <> 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) <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.
    Keith Thompson, Mar 21, 2006
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike Mimic

    strcpy

    Mike Mimic, May 16, 2004, in forum: C++
    Replies:
    9
    Views:
    802
    Peter Koch Larsen
    May 17, 2004
  2. Paul Sheer
    Replies:
    7
    Views:
    490
    Paul Sheer
    Sep 10, 2004
  3. Paul Sheer
    Replies:
    4
    Views:
    638
    Paul Sheer
    Sep 14, 2004
  4. RonHiler
    Replies:
    8
    Views:
    514
    John Harrison
    Oct 19, 2004
  5. Replies:
    11
    Views:
    926
    Alex Vinokur
    Jan 20, 2005
Loading...

Share This Page