Mark
Mark

Reputation: 1

Copy C Struct with char pointer

Firstly, I am really sorry if this has already been asked and resolved - I have spent ages searching and trying to adapt code samples to give me what I need... but sadly to no avail. Essentially I am just trying to copy the contents of one struct to another (which is documented here elsewhere but I cannot get it to work).

A scanner populates the following struct when it reads a barcode:

struct barcode
{
    char *text;
    int length;
    int id;
    int min;
    int max;
};

This is instantiated as:

static struct barcode code = {0};

I instantiate another one of the same type:

struct barcode *barcodeHolder;

This is intended to store a copy of the scanned barcode. This is because other codes will then be scanned that indicated other steps such as barcodes to indicate numbers or stages (eg. end, start, etc). Once I want to write the struct contents to disk I use the "copy" of the struct as that is what I want.

However, the char *text property always equals 'c' and not the value of the barcode.

I copy them as follows:

barcodeHolder = malloc(sizeof(code));
barcodeHolder->text = malloc(strlen(code->text) + 1);
strcpy(barcodeHolder->text, code->text);
barcodeHolder->id   = code->id;
barcodeHolder->length = code->length;
barcodeHolder->max = code->max;
barcodeHolder->min = code->min;

This is what I have got from other posts on a similar topic.

However, I am clearly doing something stupidly wrong and would welcome any help anyone might be able to offer so that my copy of the struct text element does actually get the right value copied.

Thank you!

Upvotes: 0

Views: 2128

Answers (4)

Mark
Mark

Reputation: 1

Probably unrelated, but if code in your example is a pointer to a struct, then your sizeof is wrong. It should be sizeof(*code) or sizeof(struct barcode). – Sean Bright 21 hours ago

@SeanBright In no way would I ever call myself a C programmer - ever! - so you saved completely here! Thank you. No, "code" is not a pointer in programme which confused me too... and was probably why I couldn't get anywhere. I just needed to get it work to prove we can do it for a demo. Should it progress I will come back (hopefully!) and revisit the code to better understand why it is working when it maybe shouldn't be. Thank you again!!!

Upvotes: 0

maxschlepzig
maxschlepzig

Reputation: 39235

I don't believe that your code is really:

static struct barcode code = {0};
[..]
strcpy(barcodeHolder->text, code->text);

Because the last statement would yield a compile error - because code is not a pointer you have to use code.text there (instead of code->text).

Assuming that you are actually using something like

struct barcode *code = ...;

You are allocating with your above code sizeof pointer of struct code bytes which is not enough for your structure.

Thus, copy it like this:

barcodeHolder = malloc(sizeof(struct barcode));
// alternative: ... = malloc(sizeof(*code));

*barcodeHolder = *code;
barcodeHolder->text = malloc(strlen(code->text) + 1);
strcpy(barcodeHolder->text, code->text);

Upvotes: 1

Charlie Burns
Charlie Burns

Reputation: 7056

Or, more simply:

barcodeHolder = malloc(sizeof(code));
*barcodeHolder = code;
barcodeHolder->text = strdup(code.text);

Upvotes: 0

P0W
P0W

Reputation: 47854

Your code is not a pointer

You need this:

barcodeHolder = malloc(sizeof(code));
barcodeHolder->text = malloc(strlen(code.text) + 1);
strcpy(barcodeHolder->text, code.text);
barcodeHolder->id = code.id;
barcodeHolder->length = code.length;
barcodeHolder->max = code.max;
barcodeHolder->min = code.min;

Upvotes: 3

Related Questions