C語言中的數(shù)組指針數(shù)組與函數(shù)指針數(shù)組
一、引言
在 C 語言的高級應(yīng)用中,數(shù)組指針數(shù)組和函數(shù)指針數(shù)組是兩個(gè)強(qiáng)大但容易混淆的概念。它們分別代表了 "存儲數(shù)組指針的數(shù)組" 和 "存儲函數(shù)指針的數(shù)組",在系統(tǒng)編程、嵌入式開發(fā)、游戲引擎等領(lǐng)域有著廣泛的應(yīng)用。本文將深入解析這兩個(gè)概念的語法、應(yīng)用場景及關(guān)鍵區(qū)別。
二、數(shù)組指針數(shù)組(Array of Pointers to Arrays)
1. 基本概念與語法
數(shù)組指針數(shù)組是一個(gè)數(shù)組,其元素都是指向數(shù)組的指針。這種數(shù)據(jù)結(jié)構(gòu)常用于處理多維數(shù)組的集合或動(dòng)態(tài)調(diào)整數(shù)組大小。
定義語法:
data_type (*array_name[size])[array_size];
data_type
:指向的數(shù)組元素類型array_name
:數(shù)組名稱size
:數(shù)組指針數(shù)組的大小array_size
:每個(gè)指針指向的數(shù)組大小
// 聲明:包含3個(gè)元素的數(shù)組,每個(gè)元素是指向int[4]的指針 int (*arrPtrArray[3])[4];
示例代碼:
#include <stdio.h> int main() { int arr1[3] = {1, 2, 3}; int arr2[3] = {4, 5, 6}; int arr3[3] = {7, 8, 9}; // 定義數(shù)組指針數(shù)組 int (*arr_ptr_array[3])[3] = {&arr1, &arr2, &arr3}; // 訪問數(shù)組元素 for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { printf("%d ", (*arr_ptr_array[i])[j]); } printf("\n"); } return 0; }
2.內(nèi)存模型與初始化
int matrix1[4] = {1,2,3,4}; int matrix2[4] = {5,6,7,8}; int matrix3[4] = {9,10,11,12}; // 初始化數(shù)組指針數(shù)組 int (*arrayOfPtrs[3])[4] = {&matrix1, &matrix2, &matrix3};
內(nèi)存布局:
棧內(nèi)存: +---------------------+ | arrayOfPtrs[0] | --> 指向matrix1 (地址0x1000) +---------------------+ | arrayOfPtrs[1] | --> 指向matrix2 (地址0x1010) +---------------------+ | arrayOfPtrs[2] | --> 指向matrix3 (地址0x1020) +---------------------+ 堆/數(shù)據(jù)區(qū): 0x1000: [1,2,3,4] // matrix1 0x1010: [5,6,7,8] // matrix2 0x1020: [9,10,11,12] // matrix3
3. 元素訪問技巧
// 訪問第2個(gè)矩陣的第3個(gè)元素 int val = (*arrayOfPtrs[1])[2]; // 獲取7 // 遍歷所有矩陣 for(int i=0; i<3; i++) { printf("矩陣%d: ", i+1); for(int j=0; j<4; j++) { printf("%d ", (*arrayOfPtrs[i])[j]); } printf("\n"); }
4. 數(shù)組指針數(shù)組的典型應(yīng)用
⑴. 動(dòng)態(tài)二維數(shù)組管理
使用數(shù)組指針數(shù)組動(dòng)態(tài)管理多個(gè)二維數(shù)組:
#include <stdio.h> #include <stdlib.h> int main() { // 創(chuàng)建3個(gè)4x4的二維數(shù)組 int (*matrices[3])[4][4]; for (int i = 0; i < 3; i++) { matrices[i] = (int (*)[4][4])malloc(sizeof(int[4][4])); // 初始化二維數(shù)組 for (int j = 0; j < 4; j++) { for (int k = 0; k < 4; k++) { (*matrices[i])[j][k] = i * 16 + j * 4 + k; } } } // 釋放內(nèi)存 for (int i = 0; i < 3; i++) { free(matrices[i]); } return 0; }
⑵.多維度數(shù)據(jù)處理
在科學(xué)計(jì)算中,常用于處理多組實(shí)驗(yàn)數(shù)據(jù):
// 假設(shè)有3組溫度數(shù)據(jù),每組10個(gè)測量值 float (*temperature_data[3])[10];
三、函數(shù)指針數(shù)組(Array of Function Pointers)
1. 基本概念與語法
函數(shù)指針數(shù)組是一個(gè)數(shù)組,其元素都是指向函數(shù)的指針。這種數(shù)據(jù)結(jié)構(gòu)常用于實(shí)現(xiàn)狀態(tài)機(jī)、命令處理器或回調(diào)函數(shù)表。
定義語法:
return_type (*array_name[size])(parameter_list);
return_type
:函數(shù)返回類型array_name
:數(shù)組名稱size
:數(shù)組大小parameter_list
:函數(shù)參數(shù)列表
// 聲明:包含4個(gè)函數(shù)指針的數(shù)組 // 每個(gè)函數(shù)接受int返回void void (*funcArray[4])(int);
示例代碼:
#include <stdio.h> // 定義三個(gè)不同的函數(shù) int add(int a, int b) { return a + b; } int subtract(int a, int b) { return a - b; } int multiply(int a, int b) { return a * b; } int main() { // 定義函數(shù)指針數(shù)組 int (*operations[3])(int, int) = {add, subtract, multiply}; // 通過函數(shù)指針數(shù)組調(diào)用函數(shù) printf("3 + 5 = %d\n", operations[0](3, 5)); // 輸出: 8 printf("3 - 5 = %d\n", operations[1](3, 5)); // 輸出: -2 printf("3 * 5 = %d\n", operations[2](3, 5)); // 輸出: 15 return 0; }
2.初始化與調(diào)用
// 定義不同函數(shù) void processA(int x) { printf("A處理: %d\n", x*2); } void processB(int x) { printf("B處理: %d\n", x+5); } void processC(int x) { printf("C處理: %d\n", x/2); } // 初始化數(shù)組 void (*operations[3])(int) = {processA, processB, processC}; // 動(dòng)態(tài)調(diào)用 int cmd = 0, value = 10; while(1) { printf("輸入命令(0-2, 其他退出): "); scanf("%d", &cmd); if(cmd < 0 || cmd > 2) break; operations[cmd](value); // 關(guān)鍵調(diào)用 }
內(nèi)存布局:
代碼區(qū): 0x4000: processA 機(jī)器碼 0x4100: processB 機(jī)器碼 0x4200: processC 機(jī)器碼 數(shù)據(jù)區(qū): funcArray[0] = 0x4000 funcArray[1] = 0x4100 funcArray[2] = 0x4200
3.函數(shù)指針數(shù)組的典型應(yīng)用
⑴.狀態(tài)機(jī)實(shí)現(xiàn)
使用函數(shù)指針數(shù)組實(shí)現(xiàn)簡單的狀態(tài)機(jī):
#include <stdio.h> // 定義狀態(tài)處理函數(shù)類型 typedef void (*StateHandler)(); // 狀態(tài)處理函數(shù) void state_idle() { printf("空閑狀態(tài)\n"); } void state_running() { printf("運(yùn)行狀態(tài)\n"); } void state_error() { printf("錯(cuò)誤狀態(tài)\n"); } int main() { // 狀態(tài)函數(shù)指針數(shù)組 StateHandler states[3] = {state_idle, state_running, state_error}; // 當(dāng)前狀態(tài) int current_state = 1; // 執(zhí)行當(dāng)前狀態(tài)處理函數(shù) states[current_state](); // 輸出: 運(yùn)行狀態(tài) return 0; }
⑵.命令行解析器
使用函數(shù)指針數(shù)組實(shí)現(xiàn)命令行解析器:
#include <stdio.h> #include <string.h> // 命令處理函數(shù)類型 typedef void (*CommandHandler)(); // 命令處理函數(shù) void cmd_help() { printf("顯示幫助信息\n"); } void cmd_quit() { printf("退出程序\n"); } void cmd_list() { printf("列出文件\n"); } // 命令結(jié)構(gòu) struct Command { const char* name; CommandHandler handler; }; int main() { // 命令表 struct Command commands[] = { {"help", cmd_help}, {"quit", cmd_quit}, {"list", cmd_list} }; int num_commands = sizeof(commands) / sizeof(commands[0]); char input[20]; printf("輸入命令: "); scanf("%s", input); // 查找并執(zhí)行命令 for (int i = 0; i < num_commands; i++) { if (strcmp(input, commands[i].name) == 0) { commands[i].handler(); break; } } return 0; }
四、數(shù)組指針數(shù)組與函數(shù)指針數(shù)組的區(qū)別
特性 | 數(shù)組指針數(shù)組 | 函數(shù)指針數(shù)組 |
---|---|---|
本質(zhì) | 存儲數(shù)組指針的數(shù)組 | 存儲函數(shù)指針的數(shù)組 |
語法 | data_type (*arr[size])[array_size] | return_type (*arr[size])(params) |
元素類型 | 指向數(shù)組的指針 | 指向函數(shù)的指針 |
典型應(yīng)用 | 動(dòng)態(tài)多維數(shù)組管理、科學(xué)計(jì)算 | 狀態(tài)機(jī)、命令解析器、回調(diào)函數(shù)表 |
訪問方式 | (*arr[i])[j] | arr[i](args) |
到此這篇關(guān)于C語言中的數(shù)組指針數(shù)組與函數(shù)指針數(shù)組的文章就介紹到這了,更多相關(guān)C語言數(shù)組指針數(shù)組與函數(shù)指針數(shù)組內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C 語言基礎(chǔ)實(shí)現(xiàn)青蛙跳臺階和漢諾塔問題
這篇文章我們九里講講C 語言基礎(chǔ)實(shí)現(xiàn)青蛙跳臺階和漢諾塔問題,感興趣的小伙伴可以參考下面文章的具體內(nèi)容2021-09-09C++ Vector 動(dòng)態(tài)數(shù)組的實(shí)現(xiàn)
這篇文章主要介紹了C++ Vector 動(dòng)態(tài)數(shù)組的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01C/C++ 中const關(guān)鍵字的用法小結(jié)
C++中的const關(guān)鍵字的用法非常靈活,而使用const將大大改善程序的健壯性。這篇文章主要介紹了C/C++ 中const關(guān)鍵字的用法,需要的朋友可以參考下2020-02-02C++實(shí)現(xiàn)簡單班級成績管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)簡單班級成績管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02C語言之實(shí)現(xiàn)棧的基礎(chǔ)創(chuàng)建
這篇文章主要介紹了C語言之實(shí)現(xiàn)棧的基礎(chǔ)創(chuàng)建,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07