-3

I want to make a shallow copy on an entire struct which has the constant deceleration. But I want the struct that I am copying too to be non constant.

This is what I have done so far which is producing an error:

struct Student{
    char *name;
    int age;
    Courses *list;  //First course (node)
}Student;

void shallowCopy(const Student *one){
    Student oneCopy = malloc(sizeof(one));

    oneCopy = one;     <--------------- ERROR POINTS TO THIS LINE
}

The compiler error I am getting:

Assignment discards 'const' qualifier from pointer target type.

I know I can remove the const from one or add the const to oneCopy, but I want to know if there is a way to make a shallow copy in this specific situation where Student one is a const and the copy Student oneCopy is not.

Brandon
  • 401
  • 5
  • 20
  • Why the down vote? Is this not a valid, unique question that displays what I have attempted. – Brandon Feb 15 '16 at 00:43
  • 3
    I didn't vote, but the -1 could be because there are extremely basic mistakes here that would be covered by any introductory book or course. Also you didn't post your actual code: you should get a compile error on the line `void shallowCopy(const Student *one){`; and another one on the `malloc` line, and a different error on the indicated line. – M.M Feb 15 '16 at 01:05

1 Answers1

3

It should be:

Student* oneCopy = malloc(sizeof(*one));
*oneCopy = *one;

Because you want to assign the struct, not the pointer.

Emil Laine
  • 41,598
  • 9
  • 101
  • 157