複雜到瘋掉的複數加法

 avatar
user_3763047219
c_cpp
2 years ago
1.1 kB
4
Indexable
// 2+3i real imag

struct complex {
	int real;
	int imag;
	int unuse[10000];
};

//結構型態的宣告 傳進兩個結構 回傳一個結構 
struct complex*  add_complex(struct complex *a, struct complex *b)
	//*a *b *c都是記憶體位址 指向外面的abc
{
	struct complex* c;
	c = (struct complex*)malloc(sizeof(struct complex));
	//printf("a pointer = %p\n", &a);
	(*c).real = (*a).real + (*b).real;
	(*c).imag = (*a).imag + (*b).imag;
	//return c;不用retuen c 了 直接存到c裡面
	//printf("%d\n", sizeof(a));
	//傳進去的a只用4bytes 之前的方法要傳進四萬多bytes
	return c;
}

void print_complex(struct complex c)
{
	printf("%d +%di\n", c.real, c.imag);
}

int main()
{
	struct complex a = { 1,3 }, b = { 5, 2 }, c,*cptr;
	//c = a + b;
	printf("a struct address = %p\n", &a);
	//printf("%d\n", sizeof(struct complex));
	cptr= add_complex(&a, &b);//放abc三個結構的位址
	print_complex(*cptr);
	free(cptr);//用完malloc 要把記憶體還回去
	//call by address
	// 之前的是call by value
	//print_complex(c);
	//c = a * b;

}
Editor is loading...