C++ 带数组的指针:char*getCharacterAddress(char数组[][arraySize],int行,int列):

C++ 带数组的指针:char*getCharacterAddress(char数组[][arraySize],int行,int列):,c++,arrays,function,pointers,implementation,C++,Arrays,Function,Pointers,Implementation,我只是在尝试填充function.h中的空实现,但我不知道如何填充 在main.cpp测试中,我有以下内容: TEST_CASE("Testing getCharacterAddress()"){ const unsigned int rows = 5; const unsigned int columns = 5; char first[rows][columns] = { {'f', 'i', 'r', 's', 't'}, {'s', 'e', 'c', 'o', 'n'

我只是在尝试填充function.h中的空实现,但我不知道如何填充

在main.cpp测试中,我有以下内容:

TEST_CASE("Testing getCharacterAddress()"){
const unsigned int rows = 5;
const unsigned int columns = 5;

char first[rows][columns] = {
    {'f', 'i', 'r', 's', 't'},
    {'s', 'e', 'c', 'o', 'n'},
    {'t', 'h', 'i', 'r', 'd'},
    {'f', 'o', 'u', 'r', 't'},
    {'f', 'i', 'f', 't', 'h'}
};

for (int i = 0; i < 5; i++){
    for(int j = 0; j < 5; j++){
        void* address = getCharacterAddress(first, i, j);
        INFO("Testing Address: " << address << " to contain: " << first[i][j]);
        REQUIRE(*(char*)address == first[i][j]);
    }
}
测试用例(“测试getCharacterAddress()”){
常量无符号整数行=5;
const unsigned int columns=5;
char first[行][列]={
{'f','i','r','s','t'},
{'s','e','c','o','n'},
{'t','h','i','r','d'},
{'f','o','u','r','t'},
{'f','i','f','t','h'}
};
对于(int i=0;i<5;i++){
对于(int j=0;j<5;j++){
void*address=getCharacterAddress(第一,i,j);

信息(“测试地址:”P>)在C++中使用变量,使用代码,>代码>和代码>。
int a = 1;
int* b = &a;
b
是指针,因为它指向
a
(其值是
a
)的地址

类似地,函数的实现如下所示

char* getCharacterAddress(char arr[rows][columns], int row, int col){
    return &arr[row][col];  // return the adress of the specified char
}

你想要一个2D数组条目的地址,对吗

有地址操作符
来检索对象的地址:

char* getCharacterAddress(char array[][5], size_t i, size_t j) { 
    return & array[i][j];
}

address==&first[i][j]
我可以想象。