MST

星途 面试题库

面试题:C语言指针数组作为函数参数的基础应用

编写一个函数,其参数为一个指针数组,数组中的指针指向字符串。函数的功能是计算这些字符串的总长度并返回。请用C语言实现。
29.3万 热度难度
编程语言C

知识考点

AI 面试

面试题答案

一键面试
#include <stdio.h>
#include <string.h>

int calculateTotalLength(char *strArray[], int size) {
    int totalLength = 0;
    for (int i = 0; i < size; i++) {
        totalLength += strlen(strArray[i]);
    }
    return totalLength;
}

可以使用如下方式调用:

int main() {
    char *strings[] = {"hello", "world", "test"};
    int size = sizeof(strings) / sizeof(strings[0]);
    int result = calculateTotalLength(strings, size);
    printf("Total length: %d\n", result);
    return 0;
}