S
Shivanand Kadwadkar
When structure is assigned, passed, or returned, the copying is done
monolithically. This means that the copies of any pointer fields will
point to the same place as the original. In other words, the data
pointed to is not copied.
But in case normal of normal variable they are copied to different
location.
Any idea why there is a partiality between copying normal variable and
pointer.
------------------------------------------------------------
#include<stdio.h>
#include<stdlib.h>
int main()
{
struct my
{
int *data1;
int data2;
}a,b;
a.data1=malloc(sizeof(int));
*a.data1=1;
a.data2=1;
b=a;
printf("\n %d %d", *b.data1,a.data2);
*b.data1=2;
b.data2=2;
printf("\n %d %d", *a.data1,a.data2);
printf("\n %d %d", *b.data1,b.data2);
}
Output:
1 1
2 1
2 2
--------------------------------------------
monolithically. This means that the copies of any pointer fields will
point to the same place as the original. In other words, the data
pointed to is not copied.
But in case normal of normal variable they are copied to different
location.
Any idea why there is a partiality between copying normal variable and
pointer.
------------------------------------------------------------
#include<stdio.h>
#include<stdlib.h>
int main()
{
struct my
{
int *data1;
int data2;
}a,b;
a.data1=malloc(sizeof(int));
*a.data1=1;
a.data2=1;
b=a;
printf("\n %d %d", *b.data1,a.data2);
*b.data1=2;
b.data2=2;
printf("\n %d %d", *a.data1,a.data2);
printf("\n %d %d", *b.data1,b.data2);
}
Output:
1 1
2 1
2 2
--------------------------------------------