If by "shallow copy", you mean that after assignment of a struct
containing an array, the array would point to the original struct
's data, then: it can't. Each element of the array has to be copied over to the new struct
. "Shallow copy" comes into the picture if your struct has pointers. If it doesn't, you can't do a shallow copy.
When you assign a struct
containing an array to some value, it cannot do a shallow copy, since that would mean assigning to an array, which is illegal. So the only copy you get is a deep copy.
Consider:
#include <stdio.h>
struct data {
char message[6];
};
int main(void)
{
struct data d1 = { "Hello" };
struct data d2 = d1; /* struct assignment, (almost) equivalent to
memcpy(&d2, &d1, sizeof d2) */
/* Note that it's illegal to say d2.message = d1.message */
d2.message[0] = 'h';
printf("%s
", d1.message);
printf("%s
", d2.message);
return 0;
}
The above will print:
Hello
hello
If, on the other hand, your struct
had a pointer, struct
assignment will only copy pointers, which is "shallow copy":
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct data {
char *message;
};
int main(void)
{
struct data d1, d2;
char *str = malloc(6);
if (str == NULL) {
return 1;
}
strcpy(str, "Hello");
d1.message = str;
d2 = d1;
d2.message[0] = 'h';
printf("%s
", d1.message);
printf("%s
", d2.message);
free(str);
return 0;
}
The above will print:
hello
hello
In general, given struct T d1, d2;
, d2 = d1;
is equivalent to memcpy(&d2, &d1, sizeof d2);
, but if the struct has padding, that may or may not be copied.
Edit: In C, you can't assign to arrays. Given:
int data[10] = { 0 };
int data_copy[10];
data_copy = data;
is illegal. So, as I said above, if you have an array in a struct
, assigning to the struct has to copy the data element-wise in the array. You don't get shallow copy in this case: it doesn't make any sense to apply the term "shallow copy" to a case like this.