Reputation: 293
I am trying to copy a struct into an array of the same type of struct.
My structs are
typedef struct{
int mode;
int link_cnt;
int uid;
int gid;
int size;
int pointers[NUM_INODE_POINTERS];
} inode;
typedef struct{
inode inodes[MAXFILES+1];
} inode_table;
So the inode_table is a array of inodes. I then make an instance of them:
inode_table inodetable;
inode rootinode;
Initialize the inode and copy it into the array:
inode rootinode={
.mode=0777,
.link_cnt=1,
.uid=0,
.gid=0,
.size=0,
.pointers={26,0,0,0,0,0,0,0,0,0,0,0,0}
};
memcpy(inodetable[0], &rootinode, sizeof rootinode);
This does not work and I get the error at the memcpy line:
subscripted value is neither array nor pointer nor vector
How can I copy the rootinode struct into the inodetable?
Upvotes: 1
Views: 7751
Reputation: 8579
The other answers are in error.
To perform the memcpy
version you need to pass the address of the structure not the structure.
The key line is
memcpy(inodetable.inodes+0, &rootinode, sizeof rootinode);
not
memcpy(inodetable.inodes[0], &rootinode, sizeof rootinode);
The +0
is unnecessary but just indicates where the index should go for other elements. Copy into the i
th element with:
memcpy(inodetable.inodes+i, &rootinode, sizeof rootinode);
Though as others correctly point out by definition the semantics of structure assignment in C give the same outcome:
inodetable.inodes=rootinode;
MVCE:
#include <stdio.h>
#include <memory.h>
#define NUM_INODE_POINTERS 13
#define MAXFILES 13
typedef struct{
int mode;
int link_cnt;
int uid;
int gid;
int size;
int pointers[NUM_INODE_POINTERS];
} inode;
typedef struct{
inode inodes[MAXFILES+1];
} inode_table;
int main(){
inode rootinode={
.mode=0777,
.link_cnt=1,
.uid=0,
.gid=0,
.size=0,
.pointers={26,0,0,0,0,0,0,0,0,0,0,0,0}
};
inode_table inodetable;
inodetable.inodes[0].pointers[0]=11;
printf("pointer[0]==%d\n",inodetable.inodes[0].pointers[0]);
memcpy(inodetable.inodes+0, &rootinode, sizeof rootinode);
printf("pointer[0]==%d\n",inodetable.inodes[0].pointers[0]);
return 0;
}
Upvotes: 0
Reputation: 35
I would like you to detail one of the answer for this question. Replies note that by using memcpy, only the pointers are copied. Is that possible to copy the actual values in the struct?
In the following code, I try to copy bestSol
struct into Solutions[iScenario]
for each scenario. It does not work properly, Solutions
in the generateAplan
function is correct only for the last iScenario
. For the previous ones, it keeps different values like -17866.
for (iScenario = 1; iScenario <= sce; iScenario++)
{
Solution bestSol(curSol); //struct
....
for (iRetryNumber = 0; iRetryNumber < iNRetries; iRetryNumber++)
{...
if (bestSol.m_dCost < dBestObj)
memcpy(&Solutions[iScenario], &bestSol, sizeof(bestSol));
}
if(iScenario == sce)
generateAplan(Solutions);
}
Upvotes: 0
Reputation: 4752
memcpy(inodetable.inodes[0], &rootinode, sizeof rootinode)
will work. inodetable
is a struct
type, and you can't index into those.
Another option is memcpy(&inodetable, &rootinode, sizeof rootinode)
, though it's less confusing to explicitly name the member.
You do not need to use memcpy()
to copy struct
s however. A plain assignment will work:
inodetable.inodes[0] = rootinode;
Note that this only works for struct
s, not arrays. (It will work for struct
s containing arrays too though.)
You will also need to be careful when copying struct
s containing pointers by value (via memcpy()
or plain assignment). Only the pointers themselves -- not what they're pointing to -- will be copied. The pointers in the copy end up pointing to the same place as in the copied-from struct
.
Upvotes: 3