C語言中的數(shù)組指針數(shù)組與函數(shù)指針數(shù)組
一、引言
在 C 語言的高級應用中,數(shù)組指針數(shù)組和函數(shù)指針數(shù)組是兩個強大但容易混淆的概念。它們分別代表了 "存儲數(shù)組指針的數(shù)組" 和 "存儲函數(shù)指針的數(shù)組",在系統(tǒng)編程、嵌入式開發(fā)、游戲引擎等領域有著廣泛的應用。本文將深入解析這兩個概念的語法、應用場景及關鍵區(qū)別。
二、數(shù)組指針數(shù)組(Array of Pointers to Arrays)
1. 基本概念與語法
數(shù)組指針數(shù)組是一個數(shù)組,其元素都是指向數(shù)組的指針。這種數(shù)據(jù)結構常用于處理多維數(shù)組的集合或動態(tài)調整數(shù)組大小。
定義語法:
data_type (*array_name[size])[array_size];
data_type:指向的數(shù)組元素類型array_name:數(shù)組名稱size:數(shù)組指針數(shù)組的大小array_size:每個指針指向的數(shù)組大小
// 聲明:包含3個元素的數(shù)組,每個元素是指向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.內存模型與初始化
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};內存布局:
棧內存: +---------------------+ | 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個矩陣的第3個元素
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ù)組的典型應用
⑴. 動態(tài)二維數(shù)組管理
使用數(shù)組指針數(shù)組動態(tài)管理多個二維數(shù)組:
#include <stdio.h>
#include <stdlib.h>
int main() {
// 創(chuàng)建3個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;
}
}
}
// 釋放內存
for (int i = 0; i < 3; i++) {
free(matrices[i]);
}
return 0;
}⑵.多維度數(shù)據(jù)處理
在科學計算中,常用于處理多組實驗數(shù)據(jù):
// 假設有3組溫度數(shù)據(jù),每組10個測量值 float (*temperature_data[3])[10];
三、函數(shù)指針數(shù)組(Array of Function Pointers)
1. 基本概念與語法
函數(shù)指針數(shù)組是一個數(shù)組,其元素都是指向函數(shù)的指針。這種數(shù)據(jù)結構常用于實現(xiàn)狀態(tài)機、命令處理器或回調函數(shù)表。
定義語法:
return_type (*array_name[size])(parameter_list);
return_type:函數(shù)返回類型array_name:數(shù)組名稱size:數(shù)組大小parameter_list:函數(shù)參數(shù)列表
// 聲明:包含4個函數(shù)指針的數(shù)組 // 每個函數(shù)接受int返回void void (*funcArray[4])(int);
示例代碼:
#include <stdio.h>
// 定義三個不同的函數(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ù)組調用函數(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.初始化與調用
// 定義不同函數(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};
// 動態(tài)調用
int cmd = 0, value = 10;
while(1) {
printf("輸入命令(0-2, 其他退出): ");
scanf("%d", &cmd);
if(cmd < 0 || cmd > 2) break;
operations[cmd](value); // 關鍵調用
}內存布局:
代碼區(qū): 0x4000: processA 機器碼 0x4100: processB 機器碼 0x4200: processC 機器碼 數(shù)據(jù)區(qū): funcArray[0] = 0x4000 funcArray[1] = 0x4100 funcArray[2] = 0x4200
3.函數(shù)指針數(shù)組的典型應用
⑴.狀態(tài)機實現(xiàn)
使用函數(shù)指針數(shù)組實現(xiàn)簡單的狀態(tài)機:
#include <stdio.h>
// 定義狀態(tài)處理函數(shù)類型
typedef void (*StateHandler)();
// 狀態(tài)處理函數(shù)
void state_idle() { printf("空閑狀態(tài)\n"); }
void state_running() { printf("運行狀態(tài)\n"); }
void state_error() { printf("錯誤狀態(tài)\n"); }
int main() {
// 狀態(tài)函數(shù)指針數(shù)組
StateHandler states[3] = {state_idle, state_running, state_error};
// 當前狀態(tài)
int current_state = 1;
// 執(zhí)行當前狀態(tài)處理函數(shù)
states[current_state](); // 輸出: 運行狀態(tài)
return 0;
}⑵.命令行解析器
使用函數(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"); }
// 命令結構
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ù)組 |
|---|---|---|
| 本質 | 存儲數(shù)組指針的數(shù)組 | 存儲函數(shù)指針的數(shù)組 |
| 語法 | data_type (*arr[size])[array_size] | return_type (*arr[size])(params) |
| 元素類型 | 指向數(shù)組的指針 | 指向函數(shù)的指針 |
| 典型應用 | 動態(tài)多維數(shù)組管理、科學計算 | 狀態(tài)機、命令解析器、回調函數(shù)表 |
| 訪問方式 | (*arr[i])[j] | arr[i](args) |
到此這篇關于C語言中的數(shù)組指針數(shù)組與函數(shù)指針數(shù)組的文章就介紹到這了,更多相關C語言數(shù)組指針數(shù)組與函數(shù)指針數(shù)組內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C++流程控制中用于跳轉的return和goto語句學習教程
這篇文章主要介紹了C++流程控制中用于跳轉的return和goto語句學習教程,是C++入門學習中的基礎知識,需要的朋友可以參考下2016-01-01
Qt6 QML實現(xiàn)DateTimePicker組件的示例代碼
本文展示了基于Qt6.10實現(xiàn)的DateTimePicker時間日期選擇器組件,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2025-12-12
使用C語言實現(xiàn)動態(tài)數(shù)組Vector
這篇文章主要為大家詳細介紹了使用C語言實現(xiàn)動態(tài)數(shù)組Vector的相關知識,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2024-01-01
C++?std::chrono庫使用示例(實現(xiàn)C++?獲取日期,時間戳,計時等功能)
std::chrono是C++標準庫中的一個組件,用于表示和處理時間,這篇文章主要介紹了C++?std::chrono庫使用指南(實現(xiàn)C++?獲取日期,時間戳,計時等功能),需要的朋友可以參考下2023-06-06

