语言会做一些看似合谋的事情 *看起来像你正在传递数组本身,但你真的没有。 正如理查德希思菲尔德写的那样,最简单的方法就是自己跟踪尺寸并将其传递给你的函数 额外的论点。 存在其他解决方案。 您还应该阅读comp.lang.c常见问题解答的第6部分,可在 < http://www.c-faq.com>。 - Keith Thompson(The_Other_Keith) ks***@mib.org < http://www.ghoti.net/~kst> 圣地亚哥超级计算机中心< *< http://users.sdsc.edu/~kst> 我们必须做点什么。这是事情。因此,我们必须这样做。 [snip] 一个文件有超过300k行,其中少于100行可能是兴趣。 int ScanFile(const FILE * srcFile, struct realm_structure * rlm, int * i) { / * 1.做一些非常聪明的事情来寻找匹配 2.更新结构,使其成为一个有用的矩阵 3. i ++ * / 返回0; } 这就是你在想什么? 如果我在任何一个函数中得到错误的迭代次数,我可以 最终感到困惑。但作为处理问题的一种方式,它确实很好。 Assume an array of structs that is having rows added at random. By thetime it reaches your function, you have no idea if it has a few hundredover over 10000 rows.When your function recieves this array as an argument, is there a safeway to establish how many rows that are or should I iterate over afield I know will always be used and use the final value of theiterator as the value of the array? 解决方案The trick is to keep track of how many elements the array has, and to passthat information to functions that need it. Objects are relatively cheap.Something as small as a size_t is practically free! So don''t be squeamishabout using them.--Richard Heathfield"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.ukemail: rjh at above domain (but drop the www, obviously)You need to rethink your question.It''s not possible to pass an array as an argument in C. What you cando is, for example, pass the address of (equivalently: a pointer to)an array''s first element as an argument. This pointer can then beused to access the elements of the array, but it doesn''t tell you howmany there are.The language does a few things that seemingly conspire to make it*look* like you''re passing the array itself, but you''re really not.As Richard Heathfield wrote, the most straightforward way to do thisis to keep track of the size yourself and pass it to your function asan extra argument.Other solutions exist.You should also read section 6 of the comp.lang.c FAQ, available at<http://www.c-faq.com>.--Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>We must do something. This is something. Therefore, we must do this.[snip]A file has over 300k rows of which less than 100 are likely to be ofinterest.int ScanFile(const FILE *srcFile,struct realm_structure *rlm,int *i){/*1. do something very clever to find a match2. update the struct so its a useful matrix3. i++*/return 0;}Is this what you were thinking?If i get the iteration of the count wrong in any 1 function, I couldend up confused. But as a way of dealing with the problem, it doeslook very good. 这篇关于安全地计算出数组中有多少行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-17 18:04
查看更多