I'm new to c and I have the following problem:
after executing this part of the code
TypeDefStruct tdss[10];
for(...) {
TypeDefStruct *tds = (TypeDefStruct*)malloc(sizeof(TypeDefStruct));
strcpy(&tds->data, charPointerArray[index]) // works (not original code)
memcpy((void*) &tdss[index], (void*) &tds, sizeof(TypeDefStruct)); // new
free(tds); // new
}
an error occurs here
TypeDefStruct *tds = &tdss[0]; // worked before
printf("\twith input: \"%s\"\n", tds->data); // worked before
the TypeDefStruct:
typedef struct TypeDefStruct{
char* data;
} TypeDefStruct;
also the error sigsagv occurs at some point (don't know where, may not here at all...)
what I'm trying to do
I am trying to copy this struct into an array and manage my memory correctly.
please, don't mind asking for more information!
strcpy(&tds->data, charPointerArray[index]) // works (not original code)
data
is an uninitialized object, you need to allocate an array with malloc
. Also &tds->data
argument is wrong, you need to use tds->data
.
In my case this did not solve all my errors. Also not the sigsegv.
But there is a Syntax error in
memcpy((void*) &tdss[index], (void*) &tds, sizeof(TypeDefStruct));
!
Since tds
is a type defined struct we don't need the &
. At least I think so...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.