-1

I would like to copy a variable of 1 byte to relatively larger char array, and copy it back? How can I do that?

/* memcpy example */
#include <stdio.h>
#include <string.h>

struct Data {
  unsigned char name[40];
  int age;
};

typedef unsigned char       uint8_t;
typedef unsigned short int  uint16_t;

int main ()
{

    uint8_t myval = 8;
    uint8_t myval_2 = 8;
    Data mypass;

    memcpy(&mypass.name, &myval, sizeof(uint8_t));
    memcpy(&mypass.name+1, &myval_2, sizeof(uint8_t));

    uint8_t* myvalnew = nullptr;
    uint8_t* myvalnew_2 = nullptr;

    memcpy(myvalnew, (uint8_t*)&mypass.name, sizeof(uint8_t));
    memcpy(myvalnew_2, (uint8_t*)&mypass.name+1, sizeof(uint8_t));

    return 0;

}

However I am getting errors.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Meric Ozcan
  • 678
  • 5
  • 25
  • Can you share with us the error messages? – Zoldszemesostoros Apr 02 '20 at 06:46
  • Show your errors. – Xofo Apr 02 '20 at 06:46
  • @Zoldszemesostoros segmentation fault generally – Meric Ozcan Apr 02 '20 at 06:58
  • Do not take the address of an array type. You're doing this everywhere and it's wrong. Furthermore, the last two `memcpy` calls write data to a NULL address. That's probably where your segfault is coming from. There is no need to use `memcpy` at all to set a single byte. Just do `mypass.name[0] = myval; mypass.name[1] = myval_2;` – paddy Apr 02 '20 at 07:00
  • 1
    `memcpy(myvalnew, (uint8_t*)&mypass.name, sizeof(uint8_t));` copy to a `nullptr`? Ill doom, man, Ill. – user4581301 Apr 02 '20 at 07:01
  • In your second calls to `memcpy`, you're trying to copy the values in `mypass.name` to the address 0 (`nullptr`), which will cause a segfault. – melk Apr 02 '20 at 07:02
  • COuld you please also answer this what would happen if the first element had uint16_t size? and there was a second element in array – Meric Ozcan Apr 02 '20 at 10:30
  • Why is this question tagged C++ if it's pretty much C? – Etienne de Martel Apr 03 '20 at 15:11
  • I edited the question. I changed the question also. answer is irrelevant. I would love to delete since no one wants to answers. – Meric Ozcan Apr 03 '20 at 15:37

1 Answers1

2

&mypass.name creates a pointer to your name array, you want a pointer to the first element of your array, you can use either simply mypass.name or &mypass.name[0]. memcpy is unnecessary here as you can simply assign the elements:

mypass.name[0] = myval;
mypass.name[1] = myval_2;

Your second set of memcpys will fail as your myvalnew pointers are null, I'm not sure what you're trying to achieve here. If you're trying to get the values back you can again just use normal array access:

uint8_t myvalnew = mypass.name[0];
uint8_t myvalnew_2 = mypass.name[1];
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
  • I tried to initialized this pointer since without nulptr it gave error. – Meric Ozcan Apr 02 '20 at 07:03
  • You need to initialise them to some actual memory, uninitialised and null are both incorrect – Alan Birtles Apr 02 '20 at 07:04
  • "*you want a pointer to the first element of your array*" - the address of the array and the address of the 1st element are the same address. Passing the array by name without `&` simply *decays* the array into a pointer to the 1st element. – Remy Lebeau Apr 02 '20 at 07:12
  • COuld you please also answer this what would happen if the first element had uint16_t size? and there was a second element in array – Meric Ozcan Apr 02 '20 at 10:26
  • I edited the question could u please check. I would also select you as answer – Meric Ozcan Apr 02 '20 at 10:39
  • Lastly I am using Memcopy because there will be another function which uses shared memeory between processes it is complicated. But could u please check my question again. – Meric Ozcan Apr 02 '20 at 10:47
  • You've completely changed your question which makes most of my answer irrelevant – Alan Birtles Apr 02 '20 at 12:27
  • COuld you please help me I would like to select an answer? Or we can delete the question and I can ask again – Meric Ozcan Apr 02 '20 at 14:05