如何在 C 中传递数组 "by reference"?

How to pass array "by reference" in C?

这就是我想要做的,但我的代码要么没有编译,要么给我一个意外的输出 "BC" 而不是 "B".

#include <stdio.h>

void removeFirstAndLastChar(char** string) {
    *string += 1; // Removes the first character
    int i = 0;
    for (; *string[i] != '[=11=]'; i++);
    *string[i - 1] = '[=11=]';
}

int main(void) {
    char* title = "ABC";
    removeFirstAndLastChar(&title);
    printf("%s", title);
    // Expected output: B
    return 0;
}

我在这里查看了很多与通过引用传递指针相关的答案,但其中 none 似乎包含我想在 removeFirstAndLastChar() 函数中执行的操作。

我不评价你的算法或C约定,评论你问题的朋友是完全正确的。但是,如果你仍然这样做,你可以使用这种方法。

#include <stdio.h>
#include <string.h>

void removeFirstAndLastChar(char* string) {
    memmove(string,string+1,strlen(string));
    string[strlen(string)-1]=0;
}

int main(void) {
    char title[] = "ABC";
    removeFirstAndLastChar(title);
    printf("%s", title);
    // Expected output: B
    return 0;
}