Jimmyguitaro
Jimmyguitaro

Reputation: 7

C Make copy of data from struct pointer

I have small problem with creating a copy of data.

In C I have function

struct data* copy_and_change(const struct data* input_data)

struct data contains one pointer to struct header and some integers

struct header is struct which contains information about user (chars, ints...)

struct data{
    struct header* info;  
    int salary;
};

So parameter is const struct pointer to data in memory.

My function should grab data from parameter and create copy of that data, rearrange it and return a pointer to new data (copied data)

Rearranging and doing stuff with data is no problem. problem is to make a copy of data

My question: what I should use to make copy of data?

I was trying to use memcpy(). gcc gives error argument to ‘sizeof’ in ‘memcpy’ call is the same expression as the destination; did you mean to dereference it? - probably beacuse of param is const

Upvotes: 0

Views: 420

Answers (1)

user11747064
user11747064

Reputation:

If you mean to create copy of input_data. Then why don't you create local variable put the data in it.I don't understand need of memcpy

Try this

struct  data *copy=malloc(sizeof(struct data));
*copy=*input_data;
 //your rearranging code here
 return copy;

Upvotes: 1

Related Questions