使用C覆盖数组中的字符

Oma*_*mar 0 c arrays gcc pointers

我在C中创建一个动态的二维字符数组:

注意:rows并且columns是用户输入integers

char** items;
items = (char**)malloc(rows * sizeof(char*));
int i;
for(i = 0; i < rows; i++)
{
    items[i] = (char*)malloc(columns * sizeof(char));
}

int j;
for(i = 0; i < rows; i++)
{
    for(j = 0; j < columns; j++)
    {
        items[i][j] = 'O';
    }
}
Run Code Online (Sandbox Code Playgroud)

稍后在我的代码中,我尝试覆盖数组中的特定位置:

items[arbitraryRow][arbitraryColumn] = 'S';
Run Code Online (Sandbox Code Playgroud)

但结果是该行/列中的字符现在是'SO'

我究竟做错了什么?

更新:这是我打印数组的方式:

int i;
for(i = 0; i < rows; i++)
{
    printf("[");
    int j;
    for(j = 0; j < columns; j++)
    {
        printf("'%s'", &items[i][j]);
        if(j != columns - 1)
            printf(", ");
    }
    printf("]");
    printf("\n");
}
Run Code Online (Sandbox Code Playgroud)

Tom*_*Tom 5

你没有存储你正在存储字符的字符串,所以你只能阅读一个字符,这就是S

我的怀疑是下一个字符是O,所以当你把它看成一个字符串时,你会得到SO

printf("'%c'", items[i][j]);
Run Code Online (Sandbox Code Playgroud)