954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Newb Question

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.

csteverun
Light Poster
34 posts since Sep 2007
Reputation Points: 10
Solved Threads: 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;
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Thanks for your help.

csteverun
Light Poster
34 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You