0

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.

2
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by csteverun
0

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;
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.