0

A struct node containing {... char opcode_CHAR[6]; }

a pointer declared struct node *pointer2 pointing to a node of that struct kind;

got a char temp_array[6].

j = 6 - sizeof(temp_array);

So depending on the sizeof(temp_array), I will copy to a certain index j of opcode_CHAR.
If sizeof(temp_array) == 4 ill copy to opcode_CHAR starting with j = 2;
If sizeof(temp_array) == 2 ill copy to opcode_CHAR starting with j = 4; etc...)

I am getting a compilation warning:

warning: passing argument 1 of 'strcmp' makes pointer from integer without a cast [-Wint-conversion]

because of this: strcpy(pointer2->opcode_CHAR[j], temp_array);

I tried pointer2->opcode_CHAR = pointer2->opcode_CHAR[j]; before the strcpy(pointer2->opcode_CHAR, temp_array); so that it points to the index j that I want, instead of the start of the array. But it did not work.

should I just add j like this? strcpy(pointer2->opcode_CHAR+j, temp_array);

Kami Kaze
  • 2,069
  • 15
  • 27
topcat
  • 177
  • 3
  • 17
  • Just skimming through this I would think that `strcpy(&(pointer2->opcode_CHAR[j]), temp_array);` should do the trick. Which should be the same as `strcpy(pointer2->opcode_CHAR+j, temp_array);` – Kami Kaze Apr 11 '18 at 07:04
  • @KamiKaze yep worked, clever. one forget sometimes that `->` is `*.` please answer it. – topcat Apr 11 '18 at 07:16
  • `var->` is `(*var).` to be precise. Then again it should be the same as `*var.` but I don't take chances with operator priority – Kami Kaze Apr 11 '18 at 07:24

1 Answers1

1

strcpy expects two pointers.

opcode_CHAR[j] is equivalent to *(opcode_CHAR+j) which is the value not the pointer.

This is why pointer2->opcode_CHAR = pointer2->opcode_CHAR[j]; did not work you assigned a value to pointer to the array. Luckily the compiler should tell you that the array is read only, because this would have made a big mess if you would have tried to access the array afterwards.

To get the pointer to the value you want you can do opcode_CHAR+j as you suggested or &opcode_CHAR[j] which I prefer because it is more obvious.

You obviously have to get the atribute out of the struct so you have to get to the array before dereferencing:

&(pointer2->opcode_CHAR[j])

So the complete call would be.

strcpy(pointer2->opcode_CHAR+j, temp_array);

or

strcpy(&(pointer2->opcode_CHAR[j]), temp_array);

Please make sure that your char array is zero-terminated on opcode_CHAR[5]

Kami Kaze
  • 2,069
  • 15
  • 27