3

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.

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?

Cloud
  • 18,753
  • 15
  • 79
  • 153

3 Answers3

5
typedef int (*arrayOfNPointers)[N];

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

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

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...

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

Jimbo
  • 4,352
  • 3
  • 27
  • 44
  • 1
    Yes, the actual typedef for an array of N pointers to int is simply `typedef int *arrayOfNPointers[N];` – Medinoc Apr 23 '13 at 21:07
  • Note that the malloc() is wrong, too.. he's passing in ELEMENTS to malloc without multiplying it by the size of an integer. In addition, he should not cast malloc() in a C application. – K Scott Piel Apr 23 '13 at 21:16
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.

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.

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

Community
  • 1
  • 1
K Scott Piel
  • 4,320
  • 14
  • 19
3

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

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

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

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:

/* Casting result of malloc() is not required. */
myPtrs[i] = malloc(sizeof(int) * ELEMENTS);
hmjd
  • 120,187
  • 20
  • 207
  • 252