views:

147

answers:

4

I'm using a library which for one certain feature involves variables like so:

extern const u8 foo[];
extern const u8 bar[];

I am not allowed to rename these variables in any way.

However, I like to be able to access these variables through an array (or other similar method) so that I do not need to continually hardcode new instances of these variables into my main code.

My first attempt at creating an array is as follows:

const u8* pl[] = {
    &foo,
    &bar
};

This gave me the error cannot convert 'const u8 (*)[]' to 'const u8*' in initialization, and with help elsewhere along with some Googling, I changed my array to this:

u8 (*pl)[] = {
    &foo,
    &bar
};

Upon compiling I now get the error scalar object 'pl' requires one element in initializer.

Does anyone have any ideas on what I'm doing wrong? Thanks.

+2  A: 

As the arrays don't have a size in their declaration is there any reason you can't just an array of pointers to their first elements?

E.g.

const u8* pl[] = { foo, bar };

If you wanted an array of pointers to arrays I think that you would need to do:

const u8 (*pl[])[] = { &foo, &bar };

but I don't see that it really has any advantage over the previous solution.

Charles Bailey
A reason? Well, that'd be my lack of knowledge. I'll give your solution a try and see how it goes...
a2h
+1  A: 
extern const int a[];

const int *  aa[] = { a };
anon
+5  A: 

An array of pointers to arrays only works if foo and bar have exactly the same size, and that size is known at compile time in your translation unit.

const u8 (*pl[])[32] = {&foo, &bar};

If that is not the case, you must use an array of pointers to bytes.

const u8 *pl[] = {foo, bar};
FredOverflow
You can have pointers to incomplete types (such as arrays of unknown size) in an array, you just can't have arrays of incomplete types.
Charles Bailey
@Charles And how would you use that in a meaningful way without getting an `invalid use of array with unspecified bounds` error?
FredOverflow
@FredOverflow: I'm not sure what you mean? The same way as you'd use `foo` or `bar` directly which are also arrays of unspecified bounds. Like can still do `foo[x]`, so you can do `(*pl[0])[x]` so long as `x` is actually in range. You can't check it, but if it's valid it'll work.
Charles Bailey
Alright, I'll be marking this as accepted (second snippet), but can I ask - is there any theory to be learnt in relation to pointers and arrays in this respect?
a2h
Charles Bailey
@a2h It would say the lesson is that pointers to arrays rarely make sense, but it's still a good idea to know them! Sadly, many C programmers confuse 2D arrays, arrays of pointers and pointers to arrays, based on the erroneous assumption that arrays and pointers are the same.
FredOverflow
A: 

Remove the &. An array decays normally into a pointer.

typedef int u8;  // Using int as I don't know what a u8 is.

const u8 foo[] = { 1, 2, 3};
const u8 bar[] = { 1 };

const u8* pl[] = {
    foo,
    bar
};
Martin York