T
Trying_Harder
Consider the following declaration,
#include <stdio.h>
#include <stdlib.h>
typedef struct foo {
char name[30];
int age;
}Foo;
typedef struct bar {
char * name;
int age;
}Bar;
typedef struct baz {
char name[30];
int *age;
}Baz;
int main()
{
Foo f1,f2;
Bar b1,b2;
Baz c1,c2;
/* Populating structure */
strcpy(f1.name,"JACK");
f1.age=10;
/* CASE 1 - Works fine */
f2=f1;
printf("\n F2 Members, Name %s , Age %d ", f2.name, f2.age);
printf("\n F1 Pointer %p , F2 Pointer %p ", f1, f2);
/* CASE 2 - Hmmmm.. I don't have an explanation for this */
b1.name = malloc(sizeof(char)*10);
strcpy(b1.name,"LACK");
b1.age=20;
b2=b1; /*Assignment??*/
printf("\n B2 Members, Name %s , Age %d ", b2.name, b2.age);
printf("\n B1 Pointer %p , B2 Pointer %p ", b1, b2);
/* This will print the same address out. */
printf("\n B1 Name Pointer %p , B2 Name Pointer %p ", b1.name, \
b2.name);
/* CASE 3 - As expected, does not work */
c1.age = malloc(sizeof(int));
strcpy(c1.name,"MACK");
*(c1.age)=30;
c2=c1; /*Assignment??*/
printf("\n C2 Members, Name %s , Age %d ", c2.name, c2.age);
printf("\n C1 Pointer %p , C2 Pointer %p ", c1, c2);
/* But this will print the same address out, how come? */
printf("\n C1 Name Pointer %p , C2 Name Pointer %p ", c1.age ,\
c2.age );
exit(EXIT_SUCCESS);
}
Let me begin with apologies for posting so much of code.
Clarification 1 : Is copying structures by simply assigning them, valid
(legal) portable, pedantic ?
<My Opinion> : No. If someone argues, please reason.
Clarification 2 : Why is case 2 working ?
Clarification 3 : In case 3, you will notice the address copied exactly.
Does copying address mean pointing to the same
location?
#include <stdio.h>
#include <stdlib.h>
typedef struct foo {
char name[30];
int age;
}Foo;
typedef struct bar {
char * name;
int age;
}Bar;
typedef struct baz {
char name[30];
int *age;
}Baz;
int main()
{
Foo f1,f2;
Bar b1,b2;
Baz c1,c2;
/* Populating structure */
strcpy(f1.name,"JACK");
f1.age=10;
/* CASE 1 - Works fine */
f2=f1;
printf("\n F2 Members, Name %s , Age %d ", f2.name, f2.age);
printf("\n F1 Pointer %p , F2 Pointer %p ", f1, f2);
/* CASE 2 - Hmmmm.. I don't have an explanation for this */
b1.name = malloc(sizeof(char)*10);
strcpy(b1.name,"LACK");
b1.age=20;
b2=b1; /*Assignment??*/
printf("\n B2 Members, Name %s , Age %d ", b2.name, b2.age);
printf("\n B1 Pointer %p , B2 Pointer %p ", b1, b2);
/* This will print the same address out. */
printf("\n B1 Name Pointer %p , B2 Name Pointer %p ", b1.name, \
b2.name);
/* CASE 3 - As expected, does not work */
c1.age = malloc(sizeof(int));
strcpy(c1.name,"MACK");
*(c1.age)=30;
c2=c1; /*Assignment??*/
printf("\n C2 Members, Name %s , Age %d ", c2.name, c2.age);
printf("\n C1 Pointer %p , C2 Pointer %p ", c1, c2);
/* But this will print the same address out, how come? */
printf("\n C1 Name Pointer %p , C2 Name Pointer %p ", c1.age ,\
c2.age );
exit(EXIT_SUCCESS);
}
Let me begin with apologies for posting so much of code.
Clarification 1 : Is copying structures by simply assigning them, valid
(legal) portable, pedantic ?
<My Opinion> : No. If someone argues, please reason.
Clarification 2 : Why is case 2 working ?
Clarification 3 : In case 3, you will notice the address copied exactly.
Does copying address mean pointing to the same
location?