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 7057647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:00:43+00:00 2026-05-28T04:00:43+00:00

I am doing the same thing in both the codes. In code 1: I

  • 0

I am doing the same thing in both the codes.

In code 1: I have used a char * and allocate the space using malloc in main.

In code 2: I have used a char array for the same purpose. But why is the output is different ?

Code 1:

struct node2
{
    int data;
    char p[10];
}a,b;

main()
{
    a.data = 1;

    strcpy(a.p,"stack");
    b = a;
    printf("%d %s\n",b.data,b.p);     // output 1 stack
    strcpy(b.p,"overflow"); 
    printf("%d %s\n",b.data,b.p);     // output  1 overflow
    printf("%d %s\n",a.data,a.p);     // output  1 stack
}

Code 2:

struct node1
{
    int data;
    char *p;
}a,b;

main()
{
    a.data = 1;
    a.p = malloc(100);
    strcpy(a.p,"stack");
    b = a;
    printf("%d %s\n",b.data,b.p);   //output 1 stack
    strcpy(b.p,"overflow");  
    printf("%d %s\n",b.data,b.p);   // output 1 overflow
    printf("%d  %s\n",a.data,a.p); // output 1 overflow(why not same as previous one?)  
}
  • 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-28T04:00:44+00:00Added an answer on May 28, 2026 at 4:00 am

    In the second example you’re assigning a to b, which means that a.p (char*) is being assigned to b.p. Therefore modifying the memory pointed to by b.p is also modifying the memory pointed to by a.p, since they’re both pointing to the same location in memory.

    In the first example, you have two separate arrays. Assigning a to b copies each char in the array a.p to b.p – these blocks of memory are part of the struct, they’re not pointers to a specific part in memory. Any modification to b.p in this case can’t affect a.p since they’re completely unrelated.

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

Sidebar

Related Questions

I am doing the exact same thing in two classes, and in one the
I have written the following code in my .net project. I've never used the
I have 2 applications communicating using named pipes. Main program calls client program and
I'm currently doing something like this in some code I'm working on right now:
I have done impersonation in SharePoint quite a bit in the past by doing
Learning a bit about Linq. I have the following code: (Please excuse the pathetic
I have this code: String temp = txtForm.Rtf; foreach (ReplaceStrut rs in replaceArray) {
Here's my problem. I have a middleware web service used by several web clients.
So I am pretty new to both Django and Javascript (I am using JQuery)
Suppose I have two methods, Foo and Bar , that do roughly the same

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.