Thread: Wicked riddle

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    9

    Wicked riddle

    var is array[10] of array[10] of pointer to pointer to function returning pointer to function returning pointer to pointer to array[] of pointer to array[] of pointer to int

    how do you initialize that? it's quite wicked.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Not possible. There is no way in C to declare a function that returns an array[] of anything.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    9
    I ment the afferwnt c code. I thing its something like var=*******array[10][10]; and it says that functiom returns pointer!!

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    It still won't work... the array would be destroyed as the function exits, leaving you with a pointer to nothing.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    9
    Can you explain me please what you ve had said.. I dont undwrstand.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    it's quite wicked.
    Remember your moral teaching, and eschew this wickedness!

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by grumpy View Post
    Not possible. There is no way in C to declare a function that returns an array[] of anything.
    You can return a pointer to an array. It's not very useful of course.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by King Mir View Post
    You can return a pointer to an array. It's not very useful of course.
    Not to an arbitrary sized one, which is the meaning of "array[] of" in the OP.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    2
    i've figured it out until about here: int*(*(**(*(**var[10][10] ...

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by m3rik View Post
    var is array[10] of array[10] of pointer to pointer to function returning pointer to function returning pointer to pointer to array[] of pointer to array[] of pointer to int

    how do you initialize that? it's quite wicked.
    With nullptr.

    Array and function types will wrap around the variable they name.

    An array of type T is defined as follows:
    Code:
    T (name)[X]
    Usually, the parenthesis around the name are omitted, as they are not needed. However, they are needed when you make a pointer to the array. Putting the * in front of the '(', or without the '(' would make the contained type a pointer. Putting it after '(' will make an array pointer.

    Functions are similarly defined:
    Code:
    T (name)()
    Again, the first () are optional, except when defining a function pointer, and for exactly the same reason.

    Arrays stack so that the outer type or the array is closer to the name:
    Code:
    T ((array)[X])[Y]
    This defines an array size X of array size Y of T. Again, both sets of parenthesis are optional when not further qualified.

    Functions work exactly the same way; a function returning a function pointer will have the inner parameter type list as it's list:
    Code:
    T ((foo)(int i))(double d)
    In this case foo is a int function that returns a double function, that returns a T. The param names i and d are optional, and ignored, except that i will be used in the function body. The label d is useless, but allowed for consistency. However, this is a valid type only in so far as the parser will recognize it. You cannot have a function return another function as part of a type. A '*' after the first would indicate an int function returning a pointer to double function, which is allowed.

    The type system will also allow mix and matching functions and arrays, always with the outer type closer to the name. So an array of functions would look like:
    Code:
    int ((array)[10])(int x);
    Again, you can't actually have that, only arrays of function pointers.

    Similarly a function returning an array:
    Code:
    int ((func(int x))[10]
    This is also not allowed; functions can only return pointers to arrays.


    That should give you enough information to answer your riddle. Otherwise the information is useless. If you ever actually work with nested combinations of functions and arrays, use a typedef for each contained or returned type, except multidimensional arrays.
    Last edited by King Mir; 11-04-2011 at 06:22 AM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    To follow up with King Mir, I find this site helpful when dealing with complex C declarations.

  12. #12
    Registered User
    Join Date
    Nov 2011
    Posts
    9
    So technically, var=nullptr; or what? Var=? Translate into c language. Thx for help. I find very helpful the info. Im new in programming with so many pointers.

  13. #13
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    No, King Mir meant initialize the pointers (you basically have a 10x10 array of pointers) to null pointers, as in the null pointer constant NULL. That is one way, but that makes the pointers in your array fairly useless. If you want to fill them with something meaningful, you actually have to have something of the type they point to, i.e. the stuff in red:
    Quote Originally Posted by m3rik View Post
    var is array[10] of array[10] of pointer to pointer to function returning pointer to function returning pointer to pointer to array[] of pointer to array[] of pointer to int

  14. #14
    Registered User
    Join Date
    Nov 2011
    Posts
    9
    thank you very much anduril462. You re page was very helpful. The answer was
    int *(*(**(*(**array[10][10])())())[])[];
    EDIT: And thank you also King Mir.
    Last edited by m3rik; 11-04-2011 at 12:40 PM.

  15. #15
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by m3rik View Post
    So technically, var=nullptr; or what? Var=? Translate into c language. Thx for help. I find very helpful the info. Im new in programming with so many pointers.
    I should have been paying more attention and noticed that it started with array of. Arrays are initialized with curly braces; you could initialize any array by putting equals curly braces at the end.

    But I was sort of pointing out that the word you were looking for was definition not initialization. Initialization refers to giving a variable an initial value.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Riddle me this....
    By ronaldh12 in forum C Programming
    Replies: 9
    Last Post: 10-07-2011, 12:11 PM
  2. something wicked!
    By kryptkat in forum Windows Programming
    Replies: 2
    Last Post: 06-14-2006, 03:45 PM
  3. A riddle : )
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-04-2002, 11:36 AM
  4. Riddle me this...
    By QuestionC in forum C Programming
    Replies: 2
    Last Post: 10-26-2001, 10:57 AM
  5. Riddle
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 09-30-2001, 12:13 PM