Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 597237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:15:31+00:00 2026-05-13T16:15:31+00:00

I have a structure containing character arrays with no any other member functions. I

  • 0

I have a structure containing character arrays with no any other member functions. I am doing assignment operation between two instances of these structures. If I’m not mistaken, it is doing shallow copy. Is shallow copy safe in this case?

I’ve tried this in C++ and it worked but I would just like to confirm if this behavior is safe.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:15:31+00:00Added an answer on May 13, 2026 at 4:15 pm

    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\n", d1.message);
        printf("%s\n", 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\n", d1.message);
        printf("%s\n", 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a structure array containing fields as structure arrays of varying length. For
I have a nested data structure containing objects and arrays. How can I extract
I have a JSON structure containing arrays of arrays. I have a View Model
Suppose I have a structure in C++ containing a name and a number, e.g.
I have a deep tree structure built with lists, containing lists and floats. I
I have a table containing 60 million rows. The structure is like entryid, date,
I have a data structure containing a list of objects, like this: class A
Suppose that I have a C data-structure containing many data fields (>15): struct MyData
Technology: C#, Winforms The Background: I have the following container structure (Form containing a
I have a folder structure containing a number of documents, as well as a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.