其元素又可以是一维数组,... 当你想要分配一个'char''的一维数组时, 说,你用什么指针类型? 零维是指零维。 `char *'', 不是一维`(char [N])*'',对吧?你不希望一个指针 到数组,你想要一个指向数组元素的指针 - 因此,你需要一个带有少一维指针的 ;比整个阵列。 如果通过this你的意思是导致自动重新定义,我想 你运气不好。如果是这个,你的意思是你要解决 问题,没有什么特别的聪明。关于上面概述的直截了当的解决方案,但它会更好用。 我想知道你为什么提这个。我的原始帖子清楚地表明 我希望自动执行此操作。你不知何故觉得有必要问这个问题,还有一个无可挑剔的能力,可以在 的未来中读懂我的想法。当然,有些神圣的力量有更好的事情要做,而不是在usenet新闻组上狡辩吗? 我提到我怀疑是否反对你不支持的断言 肯定会很好。我仍然怀疑。 - Eric Sosman es ***** @ acm-dot-org.inva 盖子 It sure would be nice if I could have a macro that add a level ofindirection to its argument.So if I write: AddIndirection( X )The macro AddIndirection will do: #define X (*X)so after that point whenever you use X it gets replaced by (*X)Now I KNOW you can''t have a #define generate another #define, but isthere some sneaky way to do this? Surely the cpp gurus out there canfigure out how?Thanks,grg 解决方案The reason you think this would be "nice" escapes me.What sort of use would you intend for this bletcherous idea?Yes, you can do it. But you''ll need to spell thedirective a little differently: instead ofAddIndirection(X)you must write#define X (*X).... and I suspect you will quickly come to regret writing it.--Eric Sosman es*****@acm-dot-org.invalidDid I ask for an opinion as to the nicety of this?As to the blecherosity of this, it actually has a valid use.Let''s say you''re writing a dynamic array library ( for C ). You''d likethe user to be able to write:MakeMeAnArrayWithFourDimensions( float, MyArray, 10, 20, 30, 40 )No, you need not comment on my choice of macro names.One can implement this with:#define(t,n,d1,d2,d3,d4) \typedef t n##_Type [d1][d2][d3][d4]; \typedef n##_Type * n##_Type_Ptr; \static n##_Type_Ptr n; \n = (n##_Type_Ptr) malloc( sizeof( n##_Type ) );No, I don''t need your comments about my superflous type definitions andhow clever you could squeeze this down to one line.Now the only problem is this array MyArray is actually a pointer toMyArray, so the poor user will have to write (*A) [i][j][k][q], whichis annoying. Or we could supply yet another macro : #defineAccess(n,i,j,k,q) (*n)[i][j][k][q] but that also is less thanoptimum. or we could have created a function: t n(int d1, int d2, intd3, int d4) {return * N##_Var[d1][d2][d3][d4] } .... except we''d need a separateone for returning lvalues.The only way I can see how to make the use as transparent as possibleis to do a #define MyArray(*MyArray). For those of you that thinkthis is recursive, it''s not, cpp handles this case just fine.I solicit any clever ideas as to how to do this or similar.Thanks,grgI wonder why you mention this. My original posting clearly indicatesmy wish to do this automatically. You somehow feel compelled to begthe question, plus show an unerring ability to read my mind in thefuture. Surely someone of your Godlike powers has better things to dothan quibble on usenet newsgroups?Did I ask for an opinion as to the nicety of this?No, you did not ask. You merely stated that "It surewould be nice," offering no reason why anyone would thinkso. I questioned -- and continue to question -- what youapparently take as an axiom.Second point first: Of course it isn''t recursive; that''s theway the C language is defined.As for the larger issue, your problem is caused by the wrongchoice of pointer type. Use a pointer to a 3D array to accessyour 4D array, and all the problems will disappear. Keep in mindthat C really doesn''t have multi-dimensional arrays: it has one-dimensional arrays whose elements can be one-dimensional arrays,whose elements can in turn be one-dimensional arrays, ...When you want to allocate a one-dimensional array of `char'',say, what pointer type do you use? A "zero-dimensional" `char*'',not a one-dimensional `(char[N])*'', right? You don''t want a pointerto the array, you want a pointer to the array element -- hence, youwant a pointer with "one less dimension" than the overall array.If by "this" you mean cause an automatic redefinition, I thinkyou are out of luck. If by "this" you mean you want to solve theproblem, there''s nothing especially "clever" about the straightforwardsolution outlined above, but it will work lots better.I wonder why you mention this. My original posting clearly indicatesmy wish to do this automatically. You somehow feel compelled to begthe question, plus show an unerring ability to read my mind in thefuture. Surely someone of your Godlike powers has better things to dothan quibble on usenet newsgroups?I mention my suspicion to counter your unsupported assertionthat "It sure would be nice." I remain suspicious.--Eric Sosman es*****@acm-dot-org.invalid 这篇关于如何用#define做不可能的事?>?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!