编写一个在C中分配100个字符数组的函数

发布于 2025-01-25 17:17:06 字数 86 浏览 0 评论 0原文

编写一个分配一个的函数 堆中有100个字符的阵列,如果其索引将它们设置为“ b” 可以除以9和“ 1”,否则可以将堆交给堆 大批。

我该怎么做?

Write a function that allocates an
array of 100 chars in the heap, sets them to ’b’ if their index
is divisible by 9 and to ’1’ otherwise, and deallocates the heap
array.

How do I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸵鸟症 2025-02-01 17:17:06

这应该做到,但我无法测试。

void test() {
    char* arr = (char*)malloc(100*sizeof(char));
    for(int i = 0; i < 100; i++) {
        if(i % 9 == 0) {
            arr[i] = 'b';
        } else {
            arr[i] = '1';
        }
    }
    free(arr);
}

This should do it, but I couldn't test it.

void test() {
    char* arr = (char*)malloc(100*sizeof(char));
    for(int i = 0; i < 100; i++) {
        if(i % 9 == 0) {
            arr[i] = 'b';
        } else {
            arr[i] = '1';
        }
    }
    free(arr);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文