Title: accessing pointer array within pointer structs in C
Tags: c;arrays;pointers;struct
Question: I've looked long and hard on this site for the answer to this question, but still can't seem to achieve my goal. The code compiles and works fine without the assignment statement I'm trying to add. The fact that everything is a pointer in this code is confusing the heck out of me but I can't change that, its not my code. I basically just want to override all the values of x_array (only in the first index of bs)to be the values stored in y_array. Here is the program structure:
typedef struct
double *x_array;
} b_struct;
typedef struct
b_struct *bs;
} a_struct;
void fun_1(a_struct *as);
void allocate_b(b_struct *bs);
void allocate_a(a_struct *as);
int main(void)
a_struct *as
as = (a_struct *)malloc(sizeof(a_struct));
allocate_a (as);
fun_1 (as);
// everything internal to sa is deallocated in separate functions
free (as);
return (0);
void allocate_a(a_struct *as)
int i;
if ((as-&gt;bs =(b_struct *)malloc(5*sizeof(b_struct))) == NULL)
printf("Error: Not enough memory!\n");
exit(1);
for(i=0;i<5;i++) allocate_b (&amp;(as-&gt;bs[i]));
return;
void allocate_b(b_struct *bs)
if ((bs-&gt;x_array =(double *)malloc(10*sizeof(double))) == NULL)
printf("Error: Not enough memory!\n");
exit(1);
return;
void fun_1(a_struct *as)
int i;
double y_array[10]; // the values i need are read into this array
// the following line is what will not compile
for (i=0; i<10; i++) as-&gt;bs[0]-&gt;x_array[i]=y_array[i];
return;
I have tried many permutations of adding &amp; and *() but every time I get:
error: expression must have pointer type
The code is very long and complex and I tried to just parse out what was pertinent to my specific question. I tried to make it a complete program, but I'm sorry if I botched some syntax. Hopefully, this is enough to understand what needs to happen for the assignment statement to work. Could someone please explain how to access each layer of these nested pointer structs and pointer arrays?
Comment: in this code even the main() function is written in worst possible way, it's also placed in a random position inside the body of your source code; maybe a general rethink of this would be a good thing ...

Comment: yeah, typos, sorry, `sb` should be `bs`

Comment: Your right Digikata, I think I fixed it

Comment: Ken, what makes this code poor? I am new to programming and would like to learn. Thanks

Comment: a_struct doesn't have an sb member. Is that a typo?

Comment: I'm glad this isn't YOUR code, because man it's ugly.

Comment: There is a bug in your code in allocate_a. The if statement should close after the exit(1). You could be having trouble because allocate_b never runs.  (main also has syntax errors...)

Here is the accepted answer: for (i=0; i<10; i++) as-&gt;bs[0]-&gt;x_array[i]=y_array[i];
In your code bs[0] is not a pointer, it's a b_struct. Change that line to:
for (i=0; i<10; i++)
as-&gt;bs[0].x_array[i]=y_array[i];
^^^
Comment for this answer: Thank you so much; It compiled! However, I do not understand why `bs` is not a pointer. Isn't in declared as one inside the `typedef` for `a_struct`?
Comment for this answer: Ooohhh, I see! Is it because `bs[0]` is equivalent to `*(bs + 0)` which is dereferenced?
Comment for this answer: @SPMcGee `bs` *is* a pointer. It's `bs[0]` that's not a pointer. That's how the `[]` operator works :-)
Comment for this answer: @SPMcGee That's one way of putting it.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 7, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:09/13/12', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c><arrays><pointers><struct>', 'Title': 'accessing pointer array within pointer structs in C', 'ViewCount': 2763, 'id': 12412874, 'user_AboutMe': '', 'user_AccountId': 1839720.0, 'user_DisplayName': 'SPMcGee', 'user_Reputation': 13.0, 'user_Views': 3.0, 'user_id': 1669385.0}}