I still haven't figured out why people avoid type casts like the plague. You'll 
survive through their usage. If you type cast things just to "make them work," 
then it is bad practice, but for something like that it's harmless. People 
suggested wrapping the array up in a struct/class, which requires an object 
per-array that you want to pass around, (tacky?). People have also suggested 
containers. As he hasn't gotten the hang of pointers quite yet (or so it seems 
from post), I'd assume that he'd like to just stick to a 2-d array, and not 
some "better method."


Thanks,
Tyler Littlefield
http://tysdomain.com

  ----- Original Message ----- 
  From: John Matthews 
  To: [email protected] 
  Sent: Friday, January 09, 2009 1:55 AM
  Subject: [c-prog] Re: function returning a 2D array?


  --- In [email protected], Christopher Coale <chris95...@...> wrote:
  >
  > John Matthews wrote:
  > >
  > > --- In [email protected] <mailto:c-prog%40yahoogroups.com>, 
  > > Christopher Coale <chris95219@> wrote:
  > > >
  > > > Jos Timanta Tarigan wrote:
  > > > >
  > > > > colour *col=new colour[10][10]; <-- i cant compile this line.
  > > > >
  > > > > cannot convert int(*)[10] to int*
  > > > >
  > > > You just need to typecast the "new colour[10][10]" to a "colour *".
  > > >
  > > > colour *col = (colour *)new colour[10][10];
  > >
  > > Sorry, I don't do C++, but if you needed a typecast in the equivalent
  > > C program, I would say you had done something wrong. Please tell me
  > > there's a better way :-)
  > >
  > > 
  > Why do you say that? It simply returns a pointer to the first
  element in 
  > the array, and from there you can access every element inside it.

  I've no doubt it works, but in general you should avoid typecasts -
  they are unsafe because they bypass all compiler type checking and
  allow you to do horrible things, especially where pointers are involved.

  There are situations where you need them, but for code which is as
  (relatively) simple as this, use of a typecast tells me that something
  is wrong. Just because code works, it doesn't mean it's good code, or
  even correct code.

  If your solution works and is the 'best' one, then I apologise for my
  lack of C++ knowledge. However, other posters have suggested there are
  better solutions. I would certainly be able to knock something up in C
  which works and doesn't require typecasts, which again implies to me
  that a C++ solution that does require typecasts isn't ideal.

  Cheers
  John



   

  __________ NOD32 3752 (20090108) Information __________

  This message was checked by NOD32 antivirus system.
  http://www.eset.com


[Non-text portions of this message have been removed]

Reply via email to