这里是C语言新手。我想像这样填充我的2D数组:
char * myArray[100][100]; // initialize 2D array
myArray[0] = [1,2,3] // make the first element of that 2d Array an array
为什么这在C中不起作用?我必须这样做吗?
myArray[0][0] = 1
myArray[0][1] = 2
myArray[0][2] = 3
我有一个动态的结构数组,所以我想我可以在第一个结构中存储有关该数组的信息。
因此,一个属性将表示分配给数组的内存量,另一个属性表示实际存储在数组中的结构数量。
问题是,当我把它放在一个用这些结构填充它的函数中,并在需要时尝试分配更多的内存时,原始的数组不知何故被扭曲了。
谁能解释一下这是为什么,以及如何克服它?以下是我的代码
#define INIT 3
typedef struct point{
int x;
int y;
int c;
int d;
}Point;
Point empty(){
Point p;
p.x=1;
p.y
我有一个指向数组(2d数组)的指针。我喜欢将2d数组分配给指针。
int (*p)[2]={{1,2}};//SegFault
int (*p)[2]=(int **){{1,2},{3,4}}; //should it be same as above? but also causing segFault/
int (*p)[2]=(int[][]){{5,4},{5,6}};//should it be same as above two? but also causing segFault
以及我如何访问这些值。第一个数组和第一个值的访问应该是正确的*(*(p)。// Wil
MyClass.hxx
class MyClass{
private:
int ** myArray;
};
MyClass.cxx
void MyClass::setArray()
{
int arraySize = 10;
myArray = new int*[arraySize]
for (int i = 0; i < 2; ++i) {
myArray[i] = new int[arraySize]
}
}
我的问题是,这段代码是否创建了一个2D数组,该数组被一个方向约束为两个项,另一个由arraySize约束,类似于:
in
我试图从C中的函数返回2D整数数组。我能够使用malloc()使用动态内存分配来返回它,但是我无法但很好奇如何使用静态关键字来实现它。
下面是使用静态关键字成功返回2D数组的代码片段,
int (*(get_2d_arr_using_static)())[10] // Can someone explain me this statement in detail ?
{
static int arr[10][10] = { { 1,2,3}, {4,5,6}, {7,8,9}};
return arr;
}
int main()
{
int (*arr)[10] =
我好像找不到我要找的东西。我正在学习如何在C中使用二维数组,同时也在学习指针。为2D数组声明指针的最好方法是什么?
我是这样做的
int nums[2][3] = {
{5, 4, 3},
{3, 0, 9}
};
int *numsptr = nums;
printf("%p\n", numsptr);
另外,我如何通过指针访问特定的索引?我读的那篇文章是这样说的。
In general, nums[i][j] is equivalent to *(*(nums+i)+j)
我试过这么做。
pri
在C中,如果不对第二个维度进行边界,就不可能将2D数组声明为参数,而第一个维度则可能没有边界。我想知道编译器是如何跟踪第一个维度的长度的,为什么它对进一步的维度不这样做呢?
由于编译以下代码不起作用:
int func(int array[][]) {
return 0;
}
并且会屈服
error: declaration of ‘array’ as multidimensional array must have bounds for all dimensions except the first
我有一个C函数:
void func(int n, int m)
{
int Array[n][m];
make_magic(Array);
}
生成二维数组并进行处理。我在python3.5中的ctype中使用这个函数:
from ctypes import *
magic = CDLL('lib_name')
magic.func(n,m)
那么我如何在python中获取这个2D数组呢?
我正在研究C++中的2D数组。我很难连接数组和指针的概念。我知道它们在内存分配和访问元素方面是相关的。例如
int *arr;
int num = arr + 1*sizeof(int);
是相同的
int arr[];
int num = arr[1];
我试图在2D数组和指针之间找到相同的连接--这是我的代码
void printGrid(int **arr) {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
cout << setw(3);
cout <<
有人能解释为什么在C++中声明动态2D数组时使用指向指针(**)的指针吗?我不明白为什么我们要用循环。
using namespace std;
int main(){
int **matrixPtr;//2d array declaretion
cout << "enter no of rows: ";
int row;//defining row
cin >> row;//initializing value of row from user
cout << "enter no of c
我正在为非托管C++代码编写托管C++包装器,并有一个问题。为了简单起见,假设我需要将一个2D数组从C#代码传递给托管C++到非托管C++。我没有问题的一维数组,但坚持2D版本。将其转换为1D是一种选择,但我想看看是否有其他方法。
为了简单起见,假设我希望使用中间包装器将2D数组发送到非托管代码,并更改其值。
下面是调用托管C#的C++代码:
MNumeric wrapper = new MNumeric(); //managed C++ object
int[,] dArr = new int[10, 10];
for (int i = 0; i < 10; i++)
如何从函数返回常量值的2d数组?我需要归还这样的东西:
//I don't even know if this is the best way to do 2d arrays in c++.
const int matrix[4][2] = {
{10,11},
{12,13},
{14,15},
{16,17}
};
在这里,我尝试过:
#include <iostream>
using namespace std;
int** createMatrix();
int main(){
int **m = createMatri
我有一个C++类,它有一个函数,它从同一类的另一个变量中接收一个二维数组,以便该数组的这个特定实例可以访问另一个变量。有问题的变量是一个二维数组。根据下面的代码,您将如何实现访问器函数来获取私有2D数组变量。我的意图是让访问器返回2d数组,但我不知道该使用哪种返回类型。int、int**、int*和int不起作用。
class MyClass{
public:
// gets private x variable from another MyClass variable
void proccessVar(const MyClass& aVar){
//
我试图理解C中的基本概念,我有两个问题:
1.为什么下面的代码会工作,而其他代码却不能工作?
/* This works fine */
typedef int SortTableRows[20];
SortTableRows* SortTableRowsPtr;
/* This will give error --> subscripted value is neither array nor pointer*/
int SortTableRows[20];
SortTableRows* SortTableRowsPtr;
第二个问题是第三行代码在做什么?
SortTab
我对动态内存分配和删除二维指针数组感到非常困惑。
目标是有一个2D数组,其中每个单元格都有一个指向链接列表的指针。这就是我正在做的,我没有看到任何错误,但很少警告。
警告:
1)
a value of type queue ** cannot be used to initialize an entity of type queue ***
queue* (**table) = (queue**)malloc(sizeof(queue*)*3);
2)
a value of type queue * cannot be assigned to an entity of type queue *