2

I have the following piece of code that I don't fully understand :

void (*foo[ABC]) (int *i) {
    [A] = function1,
    [B] = function2,
    [C] = function3
}

Where A, B and C are integer constants.

1- Is it a valid declaration if ABC has not been defined before?

2- What is this way of initializing called? ([A] = function1;)

3- What is the value of foo[D]? Is it a null pointer?

1
  • Any useful comments had been buried in all the noise, so I removed them all. Commented Oct 27, 2013 at 22:13

2 Answers 2

3
  1. No, it won't work if either ABC or A, B or C are not defined.
  2. The initializers are so called designated initializers (for C90 a GNU extension and standard since C99, thanks AndreyT)
  3. As long as D < ABC, foo[D] will be 0 (equivalent to a NULL-pointer), otherwise it will be undefined.

EDIT: as the question is now about what it would mean if ABCRandom and the other indexers are strings the answer is a lot shorter: it won't mean anything, using strings as array indexes is undefined behaviour.

Sign up to request clarification or add additional context in comments.

6 Comments

Designated initializers is a C language feature since C99, not a GNU extension.
What does D < ABC mean if D and ABC are strings?
@Gradient: Strings? Where did that come from? If these were strings, you wouldn't be able to use them as in your original post.
I don't think those are really strings. Seems like OP doesn't know about the existence of the word "identifier".
Also, "If it has automatic storage (such as within a function), then its value will be undefined." - no. Aggregate initialization demands that elements without an corresponding expression in the initializer list are zero-initialized (more precisely: they are "initialized as if they had static storage duration").
|
2

"I don't think it's C" is not equivalent with "it is not C".


Thanks for the link, @Kninnug -- this is a horrible C99 feature (and a GNU extension to C90), and the code has an error: it is a misspelled initialization of an array of three function pointers. I could imagine the fixed code like this:

#define ABC 3

#define A 0
#define B 1
#define C 2

void function1(int *i)
{
}

void function2(int *i)
{
}

void function3(int *i)
{
}

int main(int argc, char *argv[])
{
    void (*foo[ABC]) (int *i) = {
        [A] = function1,
        [B] = function2,
        [C] = function3
    };

    return 0;
}

This compiles.

Also:

What is the value of foo[D]? Is it a null pointer?

Well, what's D? If D >= ABC (assuming that they both are non-negative integers), then that element doesn't even exist. If D < ABC, then it is a NULL pointer, since aggregate (structure, union and array) initialization implicitly zero-initializes elements that have no corresponding initializer expression in the initializer list.

(More precisely, they are initialized "as if they had static storage duration", which is initialization to NULL in the case of pointers.)

14 Comments

Now [ABC] changed to [RandomWord].
I change my question to precise that A is NOT a number. It really is the string A.
+1 The first one commenting on this ugly syntax !
@haccks What kind of random word? Clearly that should be a size constant, else it doesn't make sense.
@AndreyT If the array indexers are strings the whole thing is undefined behaviour, prepare for hellspawn from your nose
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.