Structure copy rule in c

  • Thread starter Shivanand Kadwadkar
  • Start date
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
--------------------------------------------
 
K

Keith Thompson

Shivanand Kadwadkar said:
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.

Right. In other words, struct assignment does a "shallow copy",
not a "deep copy".
But in case normal of normal variable they are copied to different
location.

I'm not sure what you mean. Assignment for any object type simply
copies the contents of the object.
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;

You've created an object of type int and assigned the value 1 to it.
This object typically exists on "the heap"; it's not part of any
other object. (The standard doesn't use the term "heap", but it's
a common term for the region of memory managed by malloc and free.)
a.data2=1;
b=a;

a and b are two objects of type "struct my". a.data1 and b.data1
both point to the int object you created before. a.data2 and
b.data2 are two distinct objects of type int.
printf("\n %d %d", *b.data1,a.data2);
*b.data1=2;

This modifies the heap object you created above. Remember
that both a.data1 and b.data1 point to this object.
b.data2=2;

This modifies b.data2 which is part of b. It has no effect
on a.data2.
printf("\n %d %d", *a.data1,a.data2);
printf("\n %d %d", *b.data1,b.data2);
}
Output:
1 1
2 1
2 2

That's the output I'd expect. Why do you find it strange?

Finally, some style points (not relevant to the question you asked).
Your code would be easier to read if you made better use of white
space. The correct declaration for main is "int main(void)", not
"int main()" (though "int main()" is probably ok). When calling
malloc(), using the size of the allocated object rather than the
size of the type makes your code easier to maintain; you don't
have to change the call if the type changes later (e.g., "ptr =
malloc(sizeof *ptr);"). malloc() signals failure by returning
a null pointer; you should always check for this. You should
print newline characters *after* each line of output, not before.
Since main returns a result of type int, you should return a result
(C99 implicitly returns 0 for you, but it's still a good idea to
be explicit).

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
struct my
{
int *data1;
int data2;
} a, b;
a.data1 = malloc(sizeof *a.data1);
if (a.data1 == NULL) {
exit(EXIT_FAILURE);
}
*a.data1 = 1;
a.data2 = 1;
b = a;
printf("%d %d\n", *b.data1, a.data2);
*b.data1 = 2;
b.data2 = 2;
printf("%d %d\n", *a.data1, a.data2);
printf("%d %d\n", *b.data1, b.data2);
return 0;
}
 
S

Seebs

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.

Right. In both cases, the actual struct member is copied to a new
location. When it's a pointer, the pointer really is copied -- it's
just the stuff pointed to that isn't copied.
Any idea why there is a partiality between copying normal variable and
pointer.

There isn't. The normal variables and the pointers are both copied.

Also, "partiality" is not the word you want here, I'm guessing you
wanted "difference".

-s
 
K

Katie

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.


The pointer is a variable like int, char, etc. and the pointer is
copied identically. It's just a byte copy. There's no logic which says
"this is a pointer, instead of copying the byte value copy what it
points to instead".

There are lots of reasons for this, imagine you had a struct which
contained a pointer pointing to the struct, copying it would cause an
infinite loop.

Katie
 
A

arnuld

..SNIP...
Any idea why there is a partiality between copying normal variable and
pointer.


I did not know that C standard keeps a partiality between variables
and pointers. Ah... C'mon pointers have to be superior ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,266
Messages
2,571,076
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top