C 程式對比結構變數
在 C 程式語言中,結構是不同資料型別變數的集合,這些變數被分組成一個單獨的名稱下。
結構宣告和初始化
結構宣告的一般形式如下 -
datatype member1;
struct tagname{
datatype member2;
datatype member n;
};此處,
- struct 是一個關鍵字。
- tagname 指定結構的名稱。
- member1, member2 指定組成結構的資料項。
例如,
struct book{
int pages;
char author [30];
float price;
};結構變數
有三種宣告結構變數的方法,如下所示 -
第一種方法
struct book{
int pages;
char author[30];
float price;
}b;第二種方法
struct{
int pages;
char author[30];
float price;
}b;第三種方法
struct book{
int pages;
char author[30];
float price;
};
struct book b;結構的初始化和訪問
成員與結構變數之間的連結是使用成員運算子(or)點運算子建立的。
可以透過以下方法進行初始化 -
第一種方法
struct book{
int pages;
char author[30];
float price;
} b = {100, “balu”, 325.75};第二種方法
struct book{
int pages;
char author[30];
float price;
};
struct book b = {100, “balu”, 325.75};第三種方法,使用成員運算子
struct book{
int pages;
char author[30];
float price;
} ;
struct book b;
b. pages = 100;
strcpy (b.author, “balu”);
b.price = 325.75;示例
以下是用於比較結構變數的 C 程式 -
struct class{
int number;
char name[20];
float marks;
};
main(){
int x;
struct class student1 = {001,"Hari",172.50};
struct class student2 = {002,"Bobby", 167.00};
struct class student3;
student3 = student2;
x = ((student3.number == student2.number) &&
(student3.marks == student2.marks)) ? 1 : 0;
if(x == 1){
printf("
student2 and student3 are same
");
printf("%d %s %f
", student3.number,
student3.name,
student3.marks);
}
else
printf("
student2 and student3 are different
");
}輸出
當執行以上程式時,它會生成以下輸出 -
student2 and student3 are same 2 Bobby 167.000000
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
安卓
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP