You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C语言中编写操作数组的函数?

在C语言中编写数组操作函数的实用指南

嘿,我来帮你拆解下在C语言里编写数组操作函数的方法,结合你提供的代码片段来一步步说明~

首先得明确:C语言里传递数组给函数时,本质上是传递数组首元素的指针,所以我们需要同时传递数组的长度(或者当前已使用的元素数量),避免越界访问。下面针对你代码里用到的字符串数组、float数组,写几个常用的操作函数示例:

1. 向数组中添加元素的函数

你的代码里有char item[50][100](存物品名称)和float price[50](存对应价格),我们可以写一个函数来批量添加这些数据:

// 向数组添加物品和价格
void addItem(char item[][100], float price[], int *count, const char *newItem, float newPrice) {
    if (*count >= 50) { // 检查数组是否已满
        printf("警告:存储容量已达上限,无法添加新物品!\n");
        return;
    }
    strcpy(item[*count], newItem); // 复制物品名称到数组
    price[*count] = newPrice;      // 存储对应价格
    (*count)++; // 元素数量自增
}

2. 遍历显示数组元素的函数

如果需要展示所有已存储的物品和价格,可以写一个专门的显示函数:

// 显示所有物品及价格
void displayItems(const char item[][100], const float price[], int count) {
    if (count == 0) {
        printf("当前没有存储任何物品!\n");
        return;
    }
    printf("\n=== 存储的物品列表 ===\n");
    for (int i = 0; i < count; i++) {
        printf("物品%d:%s,价格:RM%.2f\n", i+1, item[i], price[i]);
    }
}

3. 整合到你的主函数中

把这些函数和你原来的代码结合起来,主函数会更清晰、模块化:

#include <stdio.h> 
#include <stdlib.h>
#include <string.h> // 别忘了包含strcpy需要的头文件

// 先声明上面的操作函数
void addItem(char item[][100], float price[], int *count, const char *newItem, float newPrice);
void displayItems(const char item[][100], const float price[], int count);

int main() { // 注意:main函数标准返回类型是int,不是void
    int storage, item_amount = 0; // item_amount记录当前物品数量,初始为0
    char input_item[50]; 
    char item[50][100]; 
    float input_price; 
    float price[50]; 

    // 存储选择逻辑(保留你原来的代码)
    printf("Storage Size:\n"); 
    printf("[1] Small Box (50cm x 50cm)-RM5 per week\n"); 
    printf("[2] Regular Box (100cm x 100cm)-RM15 per week\n"); 
    printf("[3] Large Box (200cm x 200cm)-RM25 per week\n"); 
    printf("Please choose your storage: "); 
    scanf("%i", &storage); 

    // 示例:添加几个物品
    printf("\n请输入第一个物品名称:");
    scanf("%s", input_item);
    printf("请输入该物品价格:");
    scanf("%f", &input_price);
    addItem(item, price, &item_amount, input_item, input_price);

    // 显示所有物品
    displayItems(item, price, item_amount);

    return 0;
}

// 然后实现之前声明的函数
void addItem(char item[][100], float price[], int *count, const char *newItem, float newPrice) {
    if (*count >= 50) {
        printf("警告:存储容量已达上限,无法添加新物品!\n");
        return;
    }
    strcpy(item[*count], newItem);
    price[*count] = newPrice;
    (*count)++;
}

void displayItems(const char item[][100], const float price[], int count) {
    if (count == 0) {
        printf("当前没有存储任何物品!\n");
        return;
    }
    printf("\n=== 存储的物品列表 ===\n");
    for (int i = 0; i < count; i++) {
        printf("物品%d:%s,价格:RM%.2f\n", i+1, item[i], price[i]);
    }
}

关键注意事项

  • 数组边界检查:每次操作数组前一定要判断当前元素数量是否超过数组的最大容量,避免数组越界导致的程序崩溃。
  • 参数传递:对于一维数组,函数参数可以写成float price[]或者float *price;对于二维字符串数组,必须指定第二维的大小(比如char item[][100]),否则编译器无法确定数组的内存布局。
  • const修饰符:如果函数只是读取数组内容(比如displayItems),用const修饰数组参数,可以避免意外修改数组内容,同时提升代码可读性。

内容的提问来源于stack exchange,提问作者Sarah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:00:18