我需要将棋盘存储到一个2d数组中,这个数组可以像chessBoard[row][column]那样被访问,类似于Python的工作方式,在数组中,我存储单个char元素,例如'x'。
有两个主要问题:
当我试图像char chessboard[10][10];那样声明数组时,我无法找到正确的方法来返回它.当我试图执行char **storeChessBoard(){}时,声明了类似于return chessboard;的函数,这会导致returning ‘char (*)[10]’ from a function with incompatible return type ‘c
新手问题here...why :下面的代码只适用于一维数组,而不是2D数组吗?无论b是指向一维数组还是2D数组的开始,只要它是char*指针(按原样),难道不应该有什么区别吗?我认为,一般的表示法bound1相当于界1*界2,甚至在赋值操作上也是如此。帮助?
main() //this works fine
{
char *b;
b = new char[50];
return 0;
}
。
main() //but this raises the error "Cannot convert char(*)[50] to
我正在尝试创建一个包含20行和20列的字符串表。它具有以下功能。
向表格中的单元格插入值
连接2行并替换为另一行
修改表中的值
还有更多的行动
为此,我试图声明、传递和修改2d字符串,即char指针的2D数组。我就是这么做的。下面的代码只包含我代码的一小部分。
char *first_array[20][20]; //declared 2d array of pointers for storing a table of strings
int modify(char *array1[], char *array2[]) //here i want to pass 2
我试图在我的头文件中声明一个返回2D数组的函数。在我们已经知道数组大小的情况下,如何做到这一点呢?下面是我目前正在做的事情。
class Sample
{
public:
char[x][y] getArr();
void blah(int x, int y);
private:
const static int x = 8;
const static int y = 2;
char arr[x][y];
};
在c++中,我可以创建一个固定列数的2D数组,比如5列,如下所示:
char (*c)[5];
然后,我可以为行分配内存,如下所示
c = new char[n][5];
其中n可以是任何变量,即使在运行时也可以赋值。我想知道是否以及如何使用此方法为每行动态分配可变数量的内存。也就是说,我想使用第一个语句,但可以修改第二个语句。
这是我第一次用C语言编程,在printGrid()函数中,我尝试将一些值赋给2D数组,而2D数组中的其他点只分配0。只要将值赋给数组,我就会立即测试它,并且它可以正常工作。但是,当我使用display()函数打印出整个数组时,它不能正常工作,数组中的所有值都是0。我已经花了几个小时进行搜索,并在我在这里发布问题之前自己调试了它。有人能帮我回答这个问题吗?谢谢。
#include <stdio.h>
#include <stdlib.h>
const int max_height = 4;
int height; //the height of pile
int ce
为了学习的目的,我把二维字符串数组分解成指针,在C中乱搞。
我编写了以下内容,构造了一个二维字符串数组:
#include <stdio.h>
#define ARR_SIZE 10
#define STR_LEN 20
int main() {
// Each string in our 2D array has STR_LEN bytes of space
char arr[ARR_SIZE][ARR_SIZE][STR_LEN];
for (int i = 0; i < ARR_SIZE; i++) {
for (int j
结构是
struct cloud {
//declare a pointer to point to the 2d array
char *pointer;
};
主要函数给出了(struct cloud u*)作为参数,以引用结构。
函数的其余部分涉及将值赋值给2d数组arr,然后我需要使结构中的指针指向2d数组的内存位置。
到目前为止,我已经完成了以下操作,并且结构指针的内存地址在赋值后不会改变。
printf("2d array memory address is %p\n", arr);
printf("struct array
首先,我已经回顾了以下几点:
由此可知,2D数组与char**不一样,但在内存中它们看起来完全相同。这听起来很奇怪,所以我研究了以下几点:
#include <stdio.h>
char func(char** m) {
return m[0][0]; //only works for char**, already discussed in the other SO question
}
int main() {
//char a[4][2]; //a 2D char array
int row = 4, col = 2; //char**