include <stdio.h>
//declare new structure
struct swap_num{
int a;
int b;
};
//declare new type of structure and its pointer
typedef struct {
int newA;
int newB;
}defdStruct;//, *pDefdStruct;
//function which swaps normal structure
struct swap_num swap( swap_num strt1)
{
int temp = strt1.a;
strt1.a = strt1.b;
strt1.b = temp;
return strt1;
};
//function which swap user defined structure by its pointer
defdStruct swapDefSt(defdStruct *defStrt1)//defeNew can be replaced by pDefdStruct
{
int temp = defStrt1->newA;
defStrt1->newA = defStrt1->newB;
defStrt1->newB = temp;
return *defStrt1;
}
int main()
{
//swapping normal structure
struct swap_num struct1;
struct1.a = 1;
struct1.b = 2;
printf("a = %d\n", struct1.a);
printf("b = %d\n", struct1.b);
struct1 = swap(struct1);
printf("after swap, a = %d\n", struct1.a);
printf("after swap, b = %d\n", struct1.b);
//swapping defined structure
defdStruct newStruct1;
defdStruct *ptr = &newStruct1;
newStruct1.newA = 1;
newStruct1.newB = 2;
printf("newA = %d\n", newStruct1.newA);
printf("newB = %d\n", newStruct1.newB);
*ptr = swapDefSt(ptr);
printf("after swap, newA = %d\n", struct1.a);
printf("after swap, newB = %d\n", struct1.b);
}
//Je pense que je suis genius^^
//have to go for allocating but today's weather is too GREAT to do
//TODO TODO~!!
//declare new structure
struct swap_num{
int a;
int b;
};
//declare new type of structure and its pointer
typedef struct {
int newA;
int newB;
}defdStruct;//, *pDefdStruct;
//function which swaps normal structure
struct swap_num swap( swap_num strt1)
{
int temp = strt1.a;
strt1.a = strt1.b;
strt1.b = temp;
return strt1;
};
//function which swap user defined structure by its pointer
defdStruct swapDefSt(defdStruct *defStrt1)//defeNew can be replaced by pDefdStruct
{
int temp = defStrt1->newA;
defStrt1->newA = defStrt1->newB;
defStrt1->newB = temp;
return *defStrt1;
}
int main()
{
//swapping normal structure
struct swap_num struct1;
struct1.a = 1;
struct1.b = 2;
printf("a = %d\n", struct1.a);
printf("b = %d\n", struct1.b);
struct1 = swap(struct1);
printf("after swap, a = %d\n", struct1.a);
printf("after swap, b = %d\n", struct1.b);
//swapping defined structure
defdStruct newStruct1;
defdStruct *ptr = &newStruct1;
newStruct1.newA = 1;
newStruct1.newB = 2;
printf("newA = %d\n", newStruct1.newA);
printf("newB = %d\n", newStruct1.newB);
*ptr = swapDefSt(ptr);
printf("after swap, newA = %d\n", struct1.a);
printf("after swap, newB = %d\n", struct1.b);
}
//Je pense que je suis genius^^
//have to go for allocating but today's weather is too GREAT to do
//TODO TODO~!!
댓글
댓글 쓰기