ที่นี่เราจะดูประเภทของการดำเนินการที่สามารถทำได้กับตัวแปร struct โดยพื้นฐานแล้วหนึ่งการดำเนินการสามารถทำได้สำหรับ struct การดำเนินการคือการดำเนินการมอบหมาย การดำเนินการอื่นๆ บางอย่าง เช่น การตรวจสอบความเท่าเทียมกันหรืออื่นๆ ไม่สามารถใช้กับสแต็กได้
ตัวอย่าง
#include <stdio.h>
typedef struct { //define a structure for complex objects
int real, imag;
}complex;
void displayComplex(complex c){
printf("(%d + %di)\n", c.real, c.imag);
}
main() {
complex c1 = {5, 2};
complex c2 = {8, 6};
printf("Complex numbers are:\n");
displayComplex(c1);
displayComplex(c2);
} ผลลัพธ์
Complex numbers are: (5 + 2i) (8 + 6i)
วิธีนี้ใช้ได้ดีเนื่องจากเราได้กำหนดค่าบางอย่างลงใน struct ตอนนี้ถ้าเราต้องการเปรียบเทียบสองออบเจ็กต์ struct ให้เราดูความแตกต่าง
ตัวอย่าง
#include <stdio.h>
typedef struct { //define a structure for complex objects
int real, imag;
}complex;
void displayComplex(complex c){
printf("(%d + %di)\n", c.real, c.imag);
}
main() {
complex c1 = {5, 2};
complex c2 = c1;
printf("Complex numbers are:\n");
displayComplex(c1);
displayComplex(c2);
if(c1 == c2){
printf("Complex numbers are same.");
} else {
printf("Complex numbers are not same.");
}
} ผลลัพธ์
[Error] invalid operands to binary == (have 'complex' and 'complex')