C pointer/array duality confusion

Hi all,

Can anyone provide help with getting the right syntax regarding array/pointers in C in the following code? Can't locate a specific example which clarifies this...

Say I declare a typedef to an array of pointers to some type...

/**
* An array of ptrs to sections
*/
typedef section_t* region_t[SECTION_LENGTH]; 

So now a "region_t" is an array of pointers to "section_t", "SECTION_LENGTH" long.
And now I declare a struct which contains an array of these...

typedef struct container
{
    region_t regions[N_THREADS];
}container_t;

And I have a static instance of the container type...

static container_t ctn;

In the same file as the ctn variable. I have a function which needs to access these region arrays...

region_t region = ctn.regions[nth];

But the compiler complains of an "invalid initializer". But this array would have been initialised being a member of a static variable. I don't want to initialise it, just access it. Can anyone tell me where I'm going wrong here and how, if possible to declare a variable that access the elements of "region" like a regular array variable, i.e...

  region->section_t_data_member = x;
 

Any help much appreciated. As I say, couldn't find an example which clarifies this.

Well, it is an invalid initializer -- you're trying to initialize a struct, from a pointer. You've got an array of pointers, so array[n] gives you a pointer, not a struct. To convert a pointer to a not-pointer you use the * operator.

The duality comes from the fact that a pointer and an array are both the same thing at the machine level: an address. The only difference is the number of elements(one, or many).

Thanx for your reply,

"you're trying to initialize a struct, from a pointer."

I thought with the typedef...

typedef section_t* region_t[SECTION_LENGTH];

This would mean that the following statement would be assigning a ptr to a ptr...

region_t region = ctn.regions[nth];

Because a region_t is a ptr type...or in other words, an array of section_t* ptrs. Hence my confusion, I thought in C an array and a ptr were essentially the same things, so this would work.

Anyway I got it to work by changing the assignment to the following...

section_t** region = ctn.regions[nth];

I realised that "ctn.regions[nth]" is basically an array without the subscript, which means it's a ptr. And what it points to is a ptr to a "section_t"... voila a ptr to a ptr.

And I can access the section_t* by using the subscript, i.e...

  region->section_t_data_member = x;

You're on a one-way track to segfault land if you don't understand what kind of pointers you're using. It's not enough that it compiles. Have you even allocated memory for your pointer to pointer pointers?

If I knew what you were actually trying to do I could show a way to do it.

I have allocated the memory. I've ran and tested the program and it works without seg-faulting. I'm "pretty" certain that I understand what's going on now with the pointers, although of course I might be wrong.

Thanks. I'm building a type of thread-pool, it's a pretty complex design so I didn't want to burden people with the details, but basically the code I provided is initialising the thread-pool structures which hold "task" information. Suffice to say it's currently working, but I'll watch out out seg-faults, maybe run it through valgrinde.