C++之指针函数

指针函数是将指针作为参数或返回指针作为结果的函数。指针用于间接访问和操作变量的内存位置,这在各种编程场景中都很有用,如动态内存分配、链表等数据结构和基于树的算法。

#include 
#include 
​
int* createArray(int size) {
    int* array = (int*)malloc(size * sizeof(int)); // Allocate memory for an array of integers
    return array;
}
​
void deleteArray(int* array) {
    free(array); // Deallocate the memory allocated for the array
}
​
int main() {
    int size = 5;
    int* array = createArray(size); // Create an array of integers using dynamic memory allocation
​
    // Accessing and modifying the elements of the array
    for (int i = 0; i < size; i++) {
        array[i] = i + 1;
    }
​
    printf("Array elements:\n");
    for (int i = 0; i < size; i++) {
        printf("%d\n", array[i]);
    }
    deleteArray(array); // Deallocate the memory allocated for the array
    system("pause"); // Pause the program to allow user to see the output
    return 0;
}
​
​

Array elements:

1

2

3

4

5

在本例中,createArray()函数将数组的大小作为参数,并返回一个指向动态分配的内存的指针。deleteArray()函数使用free()函数释放内存。这演示了如何使用指针来有效地管理内存。

下面是一个链表的例子:

#include 
#include 
#include 
​
struct Node {
    int data;
    struct Node* next;
};
​
struct Node* createNode(int data) {
    struct Node* newNode = new Node(); // Create a new node with the given data
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}
​
void insertNode(struct Node** head, int data) {
    struct Node* newNode = createNode(data); // Create a new node with the given data
    if (*head == NULL) {
        *head = newNode; // If the list is empty, set the new node as the head
    } else {
        newNode->next = *head; // Set the next pointer of the new node to point to the current head
        *head = newNode; // Update the head pointer to point to the new node (insertion at the beginning)
    }
}
​
void printList(struct Node* head) {
    struct Node* temp = head; // Create a temporary pointer to store the current node while traversing the list
    while (temp != NULL) { // Traverse the list until we reach the end (NULL) node
        printf("%d ", temp->data); // Print the data in each node
        temp = temp->next; // Move to the next node in the list by updating the temporary pointer's address
    }
}
​
int main() {
    struct Node* head = NULL; // Create an empty linked list with a NULL head pointer initially
    int n = 5; // Number of nodes to insert in the list (example)
    int data[] = {1, 2, 3, 4, 5}; // Array containing the data for each node (example)
    int i;
    
    for (i = 0; i < n; i++) { // Insert nodes into the linked list using their respective data from the array
        insertNode(&head, data[i]); // Pass the head pointer by reference to update it within the function call (efficient use of pointers)
    }
    
    printf("Linked list elements:"); // Print the elements of the linked list before printing its length (to know where to stop printing)
    printList(head); // Call the function to print the elements of the linked list using a pointer to traverse through it efficiently (avoiding unnecessary memory accesses)
    system("pause"); // Add this line to pause the program execution and display the output (helpful for debugging)
    return 0;
}
​

Linked list elements:5 4 3 2 1 请按任意键继续. . .

你可能感兴趣的:(c++,开发语言,指针函数)