C++ 访问typedef'的元素;指针数组

C++ 访问typedef'的元素;指针数组,c++,c,arrays,pointers,C++,C,Arrays,Pointers,我在访问传入函数的数组元素时遇到一些问题 #define N (128) #define ELEMENTS(10) typedef int (*arrayOfNPointers)[N]; 因此,如果这是正确的,它是一种数据类型,描述指向int的N指针数组 稍后,我将单独初始化阵列,如下所示: arrayOfNPointers myPtrs = { 0 }; int i; for (i=0; i<N; i++) { myPtrs[i] = (int*)malloc(ELEMENTS);

我在访问传入函数的数组元素时遇到一些问题

#define N (128)
#define ELEMENTS(10)
typedef int (*arrayOfNPointers)[N];
因此,如果这是正确的,它是一种数据类型,描述指向
int
N
指针数组

稍后,我将单独初始化阵列,如下所示:

arrayOfNPointers myPtrs = { 0 };
int i;
for (i=0; i<N; i++) {
  myPtrs[i] = (int*)malloc(ELEMENTS);
}
所以,我的语法似乎有点错误。但是在另一段代码中,我修改了一些这样的结构的内容,我没有问题

void doWork(void* input, void* output) {
   int i,m,n;
   arrayOfNPointers* inputData = (arrayOfNPointers*)input;
   int* outputData = (int*)output;

   for (m=0, n=0; n<nSamples; n++) {
      for (i=0; i<nGroups; i++) {
         outputData[m++] = (*inputData)[i][n];
      }
   }
}
void doWork(void*输入,void*输出){
int i,m,n;
arrayOfNPointers*inputData=(arrayOfNPointers*)输入;
int*输出数据=(int*)输出;
对于(m=0,n=0;n
因此,如果这是正确的,它是一种数据类型,描述指向int的N个指针的数组

我认为这是指向N个整数数组的指针,而不是指向整数的N个指针数组

这意味着下面这行的行为不符合您的预期。。。 myPtrs[i]=(int*)malloc(元素); 因为myPtrs是指向一个N维数组的指针(在本例中是128个整数的数组),所以myPtrs[i]是第i个N维数组。所以您试图将指针分配给一个数组,这就是为什么您会得到消息

错误:从类型“int*”分配给类型“int[128]”时,类型不兼容


基于
malloc()
的使用,它似乎是一个
int*
数组:

int* myPtrs[N];   /* Array of 'int*'. */
而不是指向
int[128]
数组的指针:

int (*myPtrs)[N]; /* Pointer to array of int[N]. */
是必需的。使用
malloc()
是不正确的,因为它为10字节分配内存,而不是10
int
s。更改为:

/* Casting result of malloc() is not required. */
myPtrs[i] = malloc(sizeof(int) * ELEMENTS);

我相信你要找的是以下

#define N 128
#define ELEMENTS 10
typedef int* arrayOfNPointers[N];

arrayOfNPointers myPtrs = { 0 };
int i;
for (i=0; i<N; i++) {
  myPtrs[i] = malloc(sizeof( int ) * ELEMENTS);
}
#定义N 128
#定义要素10
typedef int*数组指针[N];
arrayOfNPointers myPtrs={0};
int i;

对于(i=0;i在第二个示例中,什么类型是“pIn”和“pOut”?是的,指向int的N个指针数组的实际typedef只是
typedef int*arrayOfNPointers[N];
注意malloc()也是错误的。他将元素传递给malloc时没有将其乘以整数的大小。此外,他不应该强制转换malloc()在C应用程序中。
/* Casting result of malloc() is not required. */
myPtrs[i] = malloc(sizeof(int) * ELEMENTS);
#define N 128
#define ELEMENTS 10
typedef int* arrayOfNPointers[N];

arrayOfNPointers myPtrs = { 0 };
int i;
for (i=0; i<N; i++) {
  myPtrs[i] = malloc(sizeof( int ) * ELEMENTS);
}