1

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?

1
  • Is there a particular reason why you need a separate inode rootinode by the way? Initializing inodetable directly might be simpler otherwise. Commented Mar 22, 2015 at 19:10

4 Answers 4

3

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 structs however. A plain assignment will work:

inodetable.inodes[0] = rootinode;

Note that this only works for structs, not arrays. (It will work for structs containing arrays too though.)

You will also need to be careful when copying structs 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.

Sign up to request clarification or add additional context in comments.

3 Comments

But Note: on assignment, the compiler generated copy code is a shallow copy, as with memcpy, only the pointers (e.g. .pointers) are copied, not what they point to.
I guess the pointers in this case are more like block indices though, where it wouldn't be expected that a copy copies the blocks. :)
There's a couple of errors here. It should be memcpy(inodetable.inodes+0, &rootinode, sizeof rootinode); (+0 is redundant). And though what is there is valid memcpy(inodetable.inodes, &rootinode, sizeof rootinode) is better than memcpy(&inodetable, &rootinode, sizeof rootinode) certainly for beginners...
0

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);
     }

Comments

0

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 ith 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;
}

Comments

-1
memcpy(inodetable.inodes[0], &rootinode, sizeof rootinode);

2 Comments

This really isn't enough to warrant an answer. Add some more explanation or move this to a comment.
Try to explain the anwser!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.