C++之指针函数

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

#include <stdio.h>
#include <stdlib.h>
​
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 <stdio.h>
#include <stdlib.h>
#include <string.h>
​
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 请按任意键继续. . .

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值