I can't find the answer to this in any C reference. The question is, is there any way to do this:

#define MAX 10;

struct aStruct {
  int a;
  int b;
};

typedef struct aStruct aType;

aType array_of_atype[] = {
  {1, 2},
  {3, 4},
  {5, 6},
  {7, 8},
  {9, 10}
};

int main(int argc, char *argv[]) {

  aType *pointer_to_array_of_atype[MAX];

  pointer_to_array_of_atype = &array_of_atype;

  return 0;
}

I'm trying to have a type that I can declare a array of, then have a pointer to an array of that type. However I get error: incompatible types in assignment on the line pointer_to_array_of_atype = &array_of_atype; . Any ideas? I'm using GCC/MinGW32. Thanks for looking.

Recommended Answers

All 2 Replies

Your declaration is off. It declares pointer_to_array_of_atype to be an array of pointers to aType, not a pointer to an array. To get a pointer to an array, you wrap the asterisk and identifier in parens. But also keep in mind that the array sizes should match:

aType (*pointer_to_array_of_atype)[5];

pointer_to_array_of_atype = &array_of_atype;

You'd probably rather have a pointer to aType and then point it to the first element of the array. That way you don't have to worry about the special syntax of pointers to arrays, or the size restrictions:

aType *pointer_to_array_of_atype;

pointer_to_array_of_atype = array_of_atype;

Thanks for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.