Thread: What type is a 2D array?

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149

    What type is a 2D array?

    I have the function prototype:
    mazeL generateMazeMenu(mazeU maze[MAZEY][MAZEX]);

    Being called by the line:
    start=generateMazeMenu(maze);

    maze is defined as:
    short maze[MAZEY][MAZEX];

    MAZEY and MAZEX are both #defined as 10, although I may change that later.

    MazeU is a union that is the size of a short. In fact, one of it's members is a short.

    Everything works as it is supposed to, exept the complier gives me a warning saying that the argument of generateMazeMenu() is an incompatable pointer type. My question is, what should I cast it to to get rid of the warning?
    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.

  2. #2
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Try (MazeU[MAZEY][MAZEX]) maze

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    That gives a compile error: "cast specifies array type"
    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.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What is a 'mazeL'? You cannot return arrays.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    MazeL is a structure. It contains information about the starting point of the maze. It's short for Maze Location.
    The variable start is of type mazeL.

    Like I said, the function works exactly how I want it to work, I just want to get rid of the warning.
    Last edited by King Mir; 05-16-2006 at 05:32 PM.
    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.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Then change 'maze' to be 'mazeU maze[ ... ][ ... ]' instead of a short. Or change your prototype.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Then I would have to change the whole host of functions that expect maze to be an array of shorts.

    MazeU is prefered becouse it most accurately represents what I'm trying to do: input data as an array of shorts but read it as a structure (two actually, depending on the function).
    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
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I guess you should have planned better then.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Well you sure have been helpful.
    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.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I have. You didn't like the answer. I told you how to get rid of the warning. Change the data type the function uses, or the data type the array uses. No one said programming was going to be effortless.

    It takes planning. Plan better. The sooner you learn, the less painful it will be.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by quzah
    I have. You didn't like the answer. I told you how to get rid of the warning. Change the data type the function uses, or the data type the array uses. No one said programming was going to be effortless.

    It takes planning. Plan better. The sooner you learn, the less painful it will be.


    Quzah.
    That does not answer the question: What type conversion do I use?

    If you can positively tell me that there is no way, and explain why, that would be usefull.


    Anyway, I got rid of the warning using void pointer cast. Does anyone know if there is a better way?
    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.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Next time, try this for your original question:

    "I'm too lazy to plan this the right way, so can you please tell me a way to shut my compiler up?"


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > What type conversion do I use?
    You use the "rewrite the code" conversion like Quzah suggested.

    To answer your question, there is no quick easy magic code fix to make this problem go away.
    Your design is broken, and cannot be fixed that easily.

    > Then I would have to change the whole host of functions
    This is typical - write a whole bunch of stuff, then ask for repentance at the end.
    Just like "I've got 3 hours to hand in my homework" threads which come by every so often.

    Next time
    a) plan it better (or maybe, start with a plan)
    b) prototype ideas and check with others as to whether the approach is sound.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    It's not laziness, it's just a the time, I had not thought of using a union in this way. How often do you use unions in your code?

    But despite everything you guys have said, I have found an easy fix. I just cast it to a void pointer and the compiler stopped complaining.

    I'm still puzzled as to why C does not provide a way to cast multidimensional arrays. Logically it is a perfectly sensible operation, but there seems to be no syntax for it. Why is that so?
    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.

  15. #15
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Just because you created a work-around to the point the syntactic C parser does not flag an error or a warning does not mean it's proper and valid C or that it won't wreak havoc later on in your code.

    The parser is attempting to tell you....."Houston, we have a problem". Just like your fuel gauge tries to tell you you are running out of fuel. However, you can choose to ignore it and continue driving. Eventually though you will run out of gas and will be walking.
    You can ignore compiler warnings or try to circumvent them but eventually your code will be brought to its knees.

    Fix your design and you fix your problem.

    To me the whole setup looks odd.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. strings Vs. Char pointers
    By aijazbaig1 in forum C Programming
    Replies: 49
    Last Post: 02-13-2008, 09:51 AM
  3. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. 2d array problem with vc++
    By LiLgirL in forum C++ Programming
    Replies: 10
    Last Post: 03-16-2004, 08:17 PM