Jake Thompson said:
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.)