C 語言中的點 (.) 運算子

你好,未來的編程魔法師們!今天,我們將踏上一段令人興奮的旅程,探索 C 編程世界的奇妙點 (.) 運算子。如果你是編程新手,別擔心;我將成為你的友好導遊,我們將一步步攻克這個主題。所以,拿起你的虛擬魔杖(鍵盤),讓我們來施展一些 C 語言的魔法!

C - Dot (.) Operator

什麼是點 (.) 運算子?

C 語言中的點 (.) 運算子就像一座小型橋樑,連接你代碼中的不同部分。它用於訪問結構和聯合中的成員(變量或函數)。把它想象成一把能夠打開你代碼寶藏箱中不同隔間的鑰匙。

一個簡單的比喻

想象你有一個背包(那就是你的結構),裡面有不同的口袋(那些是你的成員)。點運算子就像你的手伸進特定口袋去抓取你所需要的東西。簡單吧?

使用點 (.) 運算子

讓我們從一個基本示例開始,看看點運算子在實際操作中是如何工作的。

#include <stdio.h>
#include <string.h>

struct Student {
char name[50];
int age;
float gpa;
};

int main() {
struct Student alice;

// 使用點運算子來訪問和賦值
strcpy(alice.name, "Alice");
alice.age = 20;
alice.gpa = 3.8;

// 使用點運算子來訪問和打印值
printf("Name: %s\n", alice.name);
printf("Age: %d\n", alice.age);
printf("GPA: %.2f\n", alice.gpa);

return 0;
}

在這個示例中,我們創建了一個 Student 結構,其中包含三個成員:nameagegpa。然後我們使用點運算子來訪問這些成員並賦值。之後,我們再次使用它來检索和打印這些值。

在結構(struct)中使用點運算子

結構就像是自定義的數據類型,可以保存不同類型的數據。點運算子是你操作這些結構的可靠工具。

#include <stdio.h>
#include <string.h>

struct Book {
char title[100];
char author[50];
int year;
};

int main() {
struct Book myFavoriteBook;

// 使用點運算子來賦值
strcpy(myFavoriteBook.title, "The C Programming Language");
strcpy(myFavoriteBook.author, "Brian Kernighan and Dennis Ritchie");
myFavoriteBook.year = 1978;

// 使用點運算子來打印值
printf("My favorite book:\n");
printf("Title: %s\n", myFavoriteBook.title);
printf("Author: %s\n", myFavoriteBook.author);
printf("Year: %d\n", myFavoriteBook.year);

return 0;
}

在這裡,我們使用點運算子來操作 Book 結構。這就像為你喜歡的書填寫圖書卡一樣!

在聯合(union)中使用點運算子

聯合與結構類似,但有個轉折——它們允許你在同一個內存位置存儲不同的數據類型。點運算子對於聯合的使用與對結構的使用一樣。

#include <stdio.h>

union Data {
int i;
float f;
char str[20];
};

int main() {
union Data data;

data.i = 10;
printf("data.i: %d\n", data.i);

data.f = 220.5;
printf("data.f: %.2f\n", data.f);

strcpy(data.str, "C Programming");
printf("data.str: %s\n", data.str);

return 0;
}

在這個示例中,我們使用點運算子來訪問聯合的不同成員。記住,在聯合中,同一時間只能有一個成員持有值!

在嵌套結構中使用點運算子

有時,我們需要創建更複雜的數據結構,通過將一個結構嵌套在另一個結構中。點運算子幫助我們在這些嵌套結構之間導航。

#include <stdio.h>

struct Date {
int day;
int month;
int year;
};

struct Employee {
char name[50];
struct Date birthdate;
float salary;
};

int main() {
struct Employee emp;

strcpy(emp.name, "John Doe");
emp.birthdate.day = 15;
emp.birthdate.month = 8;
emp.birthdate.year = 1990;
emp.salary = 50000.0;

printf("Employee Details:\n");
printf("Name: %s\n", emp.name);
printf("Birthdate: %d/%d/%d\n", emp.birthdate.day, emp.birthdate.month, emp.birthdate.year);
printf("Salary: $%.2f\n", emp.salary);

return 0;
}

在這裡,我們使用點運算子來訪問嵌套的 Date 結構內的成員。這就像在一個盒子裡打開另一個盒子一樣!

使用箭頭運算符訪問成員

現在,讓我們介紹點運算子的近親:箭頭 (->) 運算符。當使用指向結構的指針時,會使用這個運算符。

#include <stdio.h>
#include <stdlib.h>

struct Person {
char name[50];
int age;
};

int main() {
struct Person *personPtr;
personPtr = (struct Person*) malloc(sizeof(struct Person));

// 使用箭頭運算符和指針
strcpy(personPtr->name, "Bob");
personPtr->age = 30;

printf("Person Details:\n");
printf("Name: %s\n", personPtr->name);
printf("Age: %d\n", personPtr->age);

free(personPtr);
return 0;
}

在這個示例中,我們使用箭頭運算符通過指針訪問結構的成員。這就像使用遙控器(指針)來訪問電視(結構)的功能!

訪問嵌套內部結構的元素

讓我們深入嵌套結構,看看如何訪問最內層的元素。

#include <stdio.h>

struct Address {
char street[100];
char city[50];
char country[50];
};

struct Student {
char name[50];
int id;
struct Address addr;
};

int main() {
struct Student s1;

strcpy(s1.name, "Emma Watson");
s1.id = 12345;
strcpy(s1.addr.street, "123 Hogwarts Lane");
strcpy(s1.addr.city, "London");
strcpy(s1.addr.country, "UK");

printf("Student Details:\n");
printf("Name: %s\n", s1.name);
printf("ID: %d\n", s1.id);
printf("Address: %s, %s, %s\n", s1.addr.street, s1.addr.city, s1.addr.country);

return 0;
}

在這個神奇的示例中,我們使用點運算符來訪問嵌套結構的最內層元素。這就像在一個圖書館(學生)的某一章(城市)中找到特定的一頁(街道)!

年輕的編程者們,這就是 C 語言中點運算子的奇妙世界。記住,熟能生巧,所以不要害怕嘗試這些概念。祝編程愉快,願點運算子與你同在! | 運算符 | 使用方式 | 描述 | |--------|----------|------| | . (點) | structure.member | 訪問結構或聯合的成員 | | -> (箭頭) | pointer->member | 通過指針訪問結構或聯合的成員 |

Credits: Image by storyset