C-如何在2D数组中引用单个字符



我具有此功能,其目标是在2D数组上迭代并访问2D数组中的单个字符。但是char** buffer正在成为2D数组的字符串的点,而不是单个字符。

这是我的功能:

bool screen_graph_interator(screen_graph_t* local, char** buffer, uint32_t* iteration, uint32_t start_x, uint32_t start_y, uint32_t width, uint32_t height) 
{
    // local variables
    int x, y;
    // TODO: Check for valide start_x, start_y, width, height inputs againsts local screen_graph
    // get index
    x = start_x + *iteration % width;
    y = start_y + *iteration / width;
    // if we're beyond domain and range we're done iterating
    if(*iteration > (width * height)-1) {
        *buffer = NULL;
        return false;
    }
    // get char ptr
    *buffer = &(local->field[x][y]);
    (*iteration)++;
    return true;
}

我相信这线包含我的错误 *buffer = &(local->field[x][y]);

我在调试器中看到的是: new_char = 0x804a52c " XXXXXX "

其中new_char是 char* i i传递给 char** buffer

似乎new_char的值是字符串,我对吗?

当我调用printf("%snr", new_char);时,它会打印整个字符串。

编辑:

如果要运行我的代码

,这里是screen_graph_t
typedef struct screen_graph {
    char field[DISPLAY_ROWS][DISPLAY_COLS]; 
    bool expired;
} screen_graph_t;

编辑:

这是我调用screen_graph_interator的方式:

bool screen_graph_changes(screen_graph_t* old_graph, screen_graph_t* new_graph) {
    // local variables
    char* old_char = NULL;
    char* new_char = NULL;
    uint32_t iteration = 0;         // the screen_graph iteration index
    uint32_t vt_string_index = 0;    // the position in the VT100 string, to be sent to putty session
    uint32_t skip_count = 0;        // the char count not changed
    while(screen_graph_interator(old_graph, &old_char, &iteration, 0, 0, DISPLAY_COLS, DISPLAY_ROWS)) {
        iteration--;
        screen_graph_interator(new_graph, &new_char, &iteration, 0, 0, DISPLAY_COLS, DISPLAY_ROWS);
        // check for character differences
        if(   *old_char != *new_char
           && *new_char != '') {
           // determine if we need a VT100 jump
           if(skip_count > 0) {
    ...

将上面的代码片段删除并将其转换为MCVE(最小值,完整,可验证的示例),应该在问题中提供并添加一些基本断言,您可以最终以代码为代码。这样:

#include <assert.h>
#include <stdbool.h>
#include <inttypes.h>
#include <stdio.h>
enum { DISPLAY_ROWS = 24, DISPLAY_COLS = 50 };
typedef struct screen_graph
{
    char field[DISPLAY_ROWS][DISPLAY_COLS];
    bool expired;
} screen_graph_t;
static bool screen_graph_interator(screen_graph_t *local, char **buffer,
                uint32_t *iteration, uint32_t start_x, uint32_t start_y,
                uint32_t width, uint32_t height)
{
    int x = start_x + *iteration % width;
    int y = start_y + *iteration / width;
    if (*iteration > (width * height) - 1)
    {
        *buffer = NULL;
        return false;
    }
    *buffer = &(local->field[x][y]);
    assert(*buffer >= &local->field[0][0] &&
           *buffer <= &local->field[DISPLAY_ROWS-1][DISPLAY_COLS-1]);
    (*iteration)++;
    return true;
}
static bool screen_graph_changes(screen_graph_t *old_graph, screen_graph_t *new_graph)
{
    char *old_char = NULL;
    char *new_char = NULL;
    uint32_t iteration = 0;
    while (screen_graph_interator(old_graph, &old_char, &iteration, 0, 0, DISPLAY_COLS, DISPLAY_ROWS))
    {
        iteration--;
        screen_graph_interator(new_graph, &new_char, &iteration, 0, 0, DISPLAY_COLS, DISPLAY_ROWS);
        assert(old_char >= &old_graph->field[0][0] &&
               old_char <= &old_graph->field[DISPLAY_ROWS-1][DISPLAY_COLS-1]);
        assert(new_char >= &new_graph->field[0][0] &&
               new_char <= &new_graph->field[DISPLAY_ROWS-1][DISPLAY_COLS-1]);
        if (*old_char != *new_char && *new_char != '')
        {
            printf("Different: %" PRIu32 "n", iteration);
            return true;
        }
    }
    return false;
}
int main(void)
{
    screen_graph_t old_graph = { 0 };
    screen_graph_t new_graph = { 0 };
    old_graph.field[DISPLAY_ROWS-1][DISPLAY_COLS-1] = 'O';
    new_graph.field[DISPLAY_ROWS-1][DISPLAY_COLS-1] = 'N';
    screen_graph_changes(&old_graph, &new_graph);
    return 0;
}

请注意,严格地说,如果地址不超出数组的范围,则断言中的地址比较是不可靠的(

运行时,它会因断言失败而崩溃。来源在ob17.c中:

Assertion failed: (*buffer >= &local->field[0][0] && *buffer <= &local->field[DISPLAY_ROWS-1][DISPLAY_COLS-1]), function screen_graph_interator, file ob17.c, line 30.
Abort trap: 6

诊断

如果将display_rows设置为等于display_cols,则不会得到断言。他们俩在24岁时都会产生:

Different: 576

如果您在调用screen_graph_iterator()中将参数display_rows和display_cols交换,则不会得到断言。具有24和50的值,它会产生:

Different: 1200

基本问题是,您正在从界限中访问数组,因为您在screen_graph_iterator()函数中有xy的表达式。因此,解决问题的另一个解决方案使用:

#include <assert.h>
#include <stdbool.h>
#include <inttypes.h>
#include <stdio.h>
enum { DISPLAY_ROWS = 24, DISPLAY_COLS = 50 };
//enum { DISPLAY_ROWS = 24, DISPLAY_COLS = 24 };
typedef struct screen_graph
{
    char field[DISPLAY_ROWS][DISPLAY_COLS];
    bool expired;
} screen_graph_t;
static bool screen_graph_interator(screen_graph_t *local, char **buffer,
                uint32_t *iteration, uint32_t start_x, uint32_t start_y,
                uint32_t width, uint32_t height)
{
    int y = start_x + *iteration % width;
    int x = start_y + *iteration / width;
    //int x = start_x + *iteration % width;
    //int y = start_y + *iteration / width;
    if (*iteration > (width * height) - 1)
    {
        *buffer = NULL;
        return false;
    }
    *buffer = &(local->field[x][y]);
    assert(*buffer >= &local->field[0][0] &&
           *buffer <= &local->field[DISPLAY_ROWS-1][DISPLAY_COLS-1]);
    (*iteration)++;
    return true;
}
static bool screen_graph_changes(screen_graph_t *old_graph, screen_graph_t *new_graph)
{
    char *old_char = NULL;
    char *new_char = NULL;
    uint32_t iteration = 0;
    while (screen_graph_interator(old_graph, &old_char, &iteration, 0, 0, DISPLAY_COLS, DISPLAY_ROWS))
    //while (screen_graph_interator(old_graph, &old_char, &iteration, 0, 0, DISPLAY_ROWS, DISPLAY_COLS))
    {
        iteration--;
        screen_graph_interator(new_graph, &new_char, &iteration, 0, 0, DISPLAY_COLS, DISPLAY_ROWS);
        //screen_graph_interator(new_graph, &new_char, &iteration, 0, 0, DISPLAY_ROWS, DISPLAY_COLS);
        assert(old_char >= &old_graph->field[0][0] &&
               old_char <= &old_graph->field[DISPLAY_ROWS-1][DISPLAY_COLS-1]);
        assert(new_char >= &new_graph->field[0][0] &&
               new_char <= &new_graph->field[DISPLAY_ROWS-1][DISPLAY_COLS-1]);
        if (*old_char != *new_char && *new_char != '')
        {
            printf("Different: %" PRIu32 "n", iteration);
            return true;
        }
    }
    return false;
}
int main(void)
{
    screen_graph_t old_graph = { 0 };
    screen_graph_t new_graph = { 0 };
    old_graph.field[DISPLAY_ROWS-1][DISPLAY_COLS-1] = 'O';
    new_graph.field[DISPLAY_ROWS-1][DISPLAY_COLS-1] = 'N';
    screen_graph_changes(&old_graph, &new_graph);
    return 0;
}

这不会产生断言失败和Different: 1200输出。


(对于10K用户,有一个问题:无法将本地结构引用到另一个功能。它遇到了相同的问题。)

最新更新