0

I was hopping someone can help me with my C code. I'm getting this error:

error: incompatible type for argument 2 of ‘strcpy’
     strcpy(tmp, (SB->jNodes[j]));

Here's my code for where the error happens:

for (int j = 0; j < 20; j++) {
    iNode *tmp = malloc(sizeof(iNode));
    strcpy(tmp, (SB->jNodes[j]));
    if(tmp->size == -1) {
        iNode *oldRoot = SB->root;
        iNode *newShadowRoot;
        strcpy(newShadowRoot, oldRoot);
        strcpy(tmp, newShadowRoot);
        strcopy(SB->jNodes[j], tmp);
        break;
    }
    free(tmp);
}

and here's my data structures:

typedef struct iNode
{
    int mode;
    int id;
    int size;
    int pointers[NUM_POINTERS];
} iNode;


typedef struct superBlock
{
    int magic_number;
    int block_size;
    int num_blocks;
    int num_inodes;
    iNode *root;
    iNode jNodes[20];
} superBlock;
Nicky Mirfallah
  • 1,004
  • 4
  • 16
  • 38
  • 2
    `strcpy` is for copying strings. The arguments you are passing are not strings. `memcpy` is used if you want to copy arbitrary memory. Although keep in mind the destination must have memory allocated. – Michael Mior Apr 07 '17 at 19:17
  • 2
    try `*tmp = SB->jNodes[j]` instead of `strcpy(tmp, (SB->jNodes[j]))` – BLUEPIXY Apr 07 '17 at 19:20
  • 1
    I agree with @MichaelMior's comment that `memcpy()` (or `memmove()`) would be more appropriate than `strcpy()`, but still more appropriate would be straight-forward structure assignments. `*tmp = SB->nodes[j];` etc. Especially since the code `iNode *newShadowRoot; strcpy(newShadowRoot, oldRoot);` copies to an uninitialized pointer! – Jonathan Leffler Apr 07 '17 at 19:20

1 Answers1

1

Try

memcpy(tmp, &(SB->jNodes[j]), sizeof(SB->jNodes[0]));

Not strcpy since you're not copying strings.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
cleblanc
  • 3,678
  • 1
  • 13
  • 16