C语言中的结构体变量操作
这里我们将了解哪些类型的操作可以针对结构体变量执行。这里,基本上可以对结构体执行一项操作。该操作是赋值操作。栈中没有其他一些操作,如相等性检查或其他操作。
示例
#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)
由于我们已经为结构体赋予了一些值,因此这工作得很好。现在,如果我们想要比较两个结构体对象,让我们看看区别。
示例
#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')
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP