I have made a shallow copy a struct I have in the following manner:
struct Student{
char *name;
int age;
Courses *list; //First cour
In your code snippet the structure declaration is wrong. I think you mean a typedef of a structure instead of declaring an object of the structure.
For example
typedef struct Student
^^^^^^^
{
char *name;
int age;
Courses *list; //First course (node)
struct Student *friends[]; //Flexible array memeber stores other student pointers
^^^^^^^^^^^^^^
} Student;
This call of malloc is also wrong
Student *oneCopy = malloc(sizeof(one) + 20*sizeof(Student*));
There should be
Student *oneCopy = malloc(sizeof( *one ) + 20*sizeof(Student*));
^^^^^
Here is a demonstrative program that shows how the function can be written
#include
#include
#include
typedef struct Student
{
char *name;
int age;
// Courses *list; //First course (node)
struct Student *friends[]; //Flexible array memeber stores other student pointers
} Student;
Student * shallowCopy( const Student *one, size_t friends )
{
Student *oneCopy = malloc( sizeof( Student ) + friends * sizeof( Student * ) );
*oneCopy = *one;
memcpy( oneCopy->friends, one->friends, friends * sizeof( Student * ) );
return oneCopy;
}
int main( void )
{
Student *one = malloc( sizeof( Student ) + sizeof( Student * ) );
one->friends[0] = malloc( sizeof( Student ) );
one->friends[0]->age = 20;
Student *oneCopy = shallowCopy( one, 1 );
printf( "Age = %d\n", oneCopy->friends[0]->age );
free( one->friends[0] );
free( one );
free( oneCopy );
}
Its output is
Age = 20
Take into account that it is desirable that the structure also contains a data member that will store the number of elements in the flexible array.:)