动态分配二维数组
#include <stdio.h>
#include <stdlib.h>
int main() {
int rows = 3, cols = 4;
int **arr;
// 分配行指针数组
arr = (int **)malloc(rows * sizeof(int *));
if (arr == NULL) {
printf("内存分配失败\n");
return 1;
}
// 为每一行分配列指针数组
for (int i = 0; i < rows; i++) {
arr[i] = (int *)malloc(cols * sizeof(int));
if (arr[i] == NULL) {
printf("内存分配失败\n");
return 1;
}
}
// 初始化二维数组
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
arr[i][j] = i * cols + j;
}
}
// 打印二维数组
printf("二维数组内容:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
// 释放内存
for (int i = 0; i < rows; i++) {
free(arr[i]);
}
free(arr);
return 0;
}
指针数组
#include <stdio.h>
int main() {
const char *messages[] = {
"Hello, World!",
"Welcome to Embedded Systems",
"Pointer Arrays in C"
};
int n = sizeof(messages) / sizeof(messages[0]);
for (int i = 0; i < n; i++) {
printf("%s\n", messages[i]);
}
return 0;
}
函数指针数组
#include <stdio.h>
void func1() { printf("Function 1\n"); }
void func2() { printf("Function 2\n"); }
void func3() { printf("Function 3\n"); }
int main() {
void (*func_arr[])() = {func1, func2, func3};
int n = sizeof(func_arr) / sizeof(func_arr[0]);
for (int i = 0; i < n; i++) {
func_arr[i]();
}
return 0;
}
操作复杂数据结构
二级指针常用于处理复杂数据结构,如多级链表或树结构
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
struct Node** children;
int child_count;
} Node;
// 创建新节点
Node* createNode(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
newNode->children = NULL;
newNode->child_count = 0;
return newNode;
}
// 添加子节点
void addChild(Node* parent, Node* child) {
parent->children = (Node**)realloc(parent->children, (parent->child_count + 1) * sizeof(Node*));
parent->children[parent->child_count] = child;
parent->child_count++;
}
// 打印节点及其子节点
void printNode(Node* node) {
printf("Node data: %d\n", node->data);
printf("Children: ");
for (int i = 0; i < node->child_count; i++) {
printf("%d ", node->children[i]->data);
}
printf("\n");
}
int main() {
Node* root = createNode(1);
Node* child1 = createNode(2);
Node* child2 = createNode(3);
addChild(root, child1);
addChild(root, child2);
printNode(root);
free(root->children);
free(root);
free(child1);
free(child2);
return 0;
}
动态管理指针数组
二级指针可以动态管理一维指针数组,如在插入、删除元素时调整数组大小。
#include <stdio.h>
#include <stdlib.h>
// 添加新元素到指针数组
void addElement(int ***arr, int *size, int *newElement) {
*arr = (int **)realloc(*arr, (*size + 1) * sizeof(int *));
(*arr)[*size] = newElement;
(*size)++;
}
// 删除指针数组中的元素
void removeElement(int ***arr, int *size, int index) {
if (index < 0 || index >= *size) return;
for (int i = index; i < *size - 1; i++) {
(*arr)[i] = (*arr)[i + 1];
}
*arr = (int **)realloc(*arr, (*size - 1) * sizeof(int *));
(*size)--;
}
int main() {
int **arr = NULL;
int size = 0;
int a = 10, b = 20, c = 30;
addElement(&arr, &size, &a);
addElement(&arr, &size, &b);
addElement(&arr, &size, &c);
printf("数组内容: ");
for (int i = 0; i < size; i++) {
printf("%d ", *arr[i]);
}
printf("\n");
removeElement(&arr, &size, 1); // 删除第二个元素
printf("删除后的数组内容: ");
for (int i = 0; i < size; i++) {
printf("%d ", *arr[i]);
}
printf("\n");
free(arr);
return 0;
}