访问指针类型定义数组的元素

时间:2022-09-26 21:31:36

I'm having some issues accessing elements of an array passed into a function.

我在访问传递给函数的数组元素时遇到了一些问题。

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

So, if this is right, it is a data type describing an array of N pointers to int.

如果这是对的,它是一种数据类型,描述了N个指向int的指针的数组。

I later initialize my arrays individually, like so:

之后我分别初始化数组,如下所示:

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

However, this fails with the following error:

但是,这样做失败了,错误如下:

error: incompatible types when assigning to type 'int[128]' from type 'int *'

So, it seems there is something very wrong in my syntax. But in another chunk of code, where I'm modifying the contents of some such structures, I have no problems.

所以,我的语法好像有点问题。但是在另一段代码中,我修改了一些这样的结构的内容,我没有问题。

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];
      }
   }
}

Is this array logic severely broken?

这个数组逻辑严重破坏了吗?

3 个解决方案

#1


4  

I believe what you're looking for is the following...

我相信你在寻找的是以下……

#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);
}

You want arrayOfPointer to be an array of N pointers to ELEMENTS integers. Also, when you malloc() the space for your integers, you need to multiply the number of ELEMENTS by the size of an integer. As it is, the space you're allocating is too small to hold the data you're trying to store in it.

您希望arrayOfPointer是指向元素整数的N个指针的数组。此外,当您malloc()整数的空间时,需要将元素的数量乘以一个整数的大小。实际上,您正在分配的空间太小,无法保存要存储的数据。

Your typedef declared arrayOfPointer as a pointer to an array of N integers. Remember to use the right-left reading rule to understand what you are declaring a variable/type to be. Because you had (*arrayOfPointer) in parens there was nothing to the right and a pointer to the left, so arrayOfPointer is a pointer TO [N] (right) int (left). Not what you intended.

您的typedef声明了arrayOfPointer作为一个指向N个整数数组的指针。记住要使用右向左读取规则来理解要声明的变量/类型是什么。因为在parens中有(*arrayOfPointer)右边没有指针,左边没有指针,所以arrayOfPointer是指向[N](右)int(左)的指针。不是你的目的的。

Also... do not cast malloc() in C!

也……不要在C中使用malloc() !

#2


5  

typedef int (*arrayOfNPointers)[N];

So, if this is right, it is a data type describing an array of N pointers to int.

如果这是对的,它是一种数据类型,描述了N个指向int的指针的数组。

I think this is a pointer to an array of N integers and not an array of N pointers to integers....

我认为这是一个指针指向一个N个整数数组而不是N指针整数数组....

This means that that the following line doesn't behave as your expecting... myPtrs[i] = (int*)malloc(ELEMENTS); Because myPtrs is a pointer to an N-dimensional array (in this case an array of 128 ints), myPtrs[i] is the i-th n-dimensional array. So you are trying to assign a pointer to an array, which is why you get the msg...

这意味着下面这句话并没有表现出你的期望……myPtrs[我]=(int *)malloc(元素);因为myPtrs是一个指向n维数组的指针(在本例中为128个ints数组),myPtrs[i]是第i个n维数组。所以你尝试分配一个指向数组的指针,这就是为什么你会得到msg…

error: incompatible types when assigning to type 'int[128]' from type 'int *'

错误:在将'int[128]'类型赋值为'int *'类型时,不兼容的类型

#3


3  

Based on the use of malloc() it seems an array of int*:

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

int* myPtrs[N];   /* Array of 'int*'. */

and not a pointer to an int[128] array:

而不是指向int[128]数组的指针:

int (*myPtrs)[N]; /* Pointer to array of int[N]. */

is required. The use of malloc() is incorrect as it allocating memory for 10 bytes and not 10 ints. Change to:

是必需的。malloc()的使用是不正确的,因为它为10个字节分配内存,而不是10个字节。改变:

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

#1


4  

I believe what you're looking for is the following...

我相信你在寻找的是以下……

#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);
}

You want arrayOfPointer to be an array of N pointers to ELEMENTS integers. Also, when you malloc() the space for your integers, you need to multiply the number of ELEMENTS by the size of an integer. As it is, the space you're allocating is too small to hold the data you're trying to store in it.

您希望arrayOfPointer是指向元素整数的N个指针的数组。此外,当您malloc()整数的空间时,需要将元素的数量乘以一个整数的大小。实际上,您正在分配的空间太小,无法保存要存储的数据。

Your typedef declared arrayOfPointer as a pointer to an array of N integers. Remember to use the right-left reading rule to understand what you are declaring a variable/type to be. Because you had (*arrayOfPointer) in parens there was nothing to the right and a pointer to the left, so arrayOfPointer is a pointer TO [N] (right) int (left). Not what you intended.

您的typedef声明了arrayOfPointer作为一个指向N个整数数组的指针。记住要使用右向左读取规则来理解要声明的变量/类型是什么。因为在parens中有(*arrayOfPointer)右边没有指针,左边没有指针,所以arrayOfPointer是指向[N](右)int(左)的指针。不是你的目的的。

Also... do not cast malloc() in C!

也……不要在C中使用malloc() !

#2


5  

typedef int (*arrayOfNPointers)[N];

So, if this is right, it is a data type describing an array of N pointers to int.

如果这是对的,它是一种数据类型,描述了N个指向int的指针的数组。

I think this is a pointer to an array of N integers and not an array of N pointers to integers....

我认为这是一个指针指向一个N个整数数组而不是N指针整数数组....

This means that that the following line doesn't behave as your expecting... myPtrs[i] = (int*)malloc(ELEMENTS); Because myPtrs is a pointer to an N-dimensional array (in this case an array of 128 ints), myPtrs[i] is the i-th n-dimensional array. So you are trying to assign a pointer to an array, which is why you get the msg...

这意味着下面这句话并没有表现出你的期望……myPtrs[我]=(int *)malloc(元素);因为myPtrs是一个指向n维数组的指针(在本例中为128个ints数组),myPtrs[i]是第i个n维数组。所以你尝试分配一个指向数组的指针,这就是为什么你会得到msg…

error: incompatible types when assigning to type 'int[128]' from type 'int *'

错误:在将'int[128]'类型赋值为'int *'类型时,不兼容的类型

#3


3  

Based on the use of malloc() it seems an array of int*:

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

int* myPtrs[N];   /* Array of 'int*'. */

and not a pointer to an int[128] array:

而不是指向int[128]数组的指针:

int (*myPtrs)[N]; /* Pointer to array of int[N]. */

is required. The use of malloc() is incorrect as it allocating memory for 10 bytes and not 10 ints. Change to:

是必需的。malloc()的使用是不正确的,因为它为10个字节分配内存,而不是10个字节。改变:

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