数组 - 他们完成的地方? [英] arrays - where they finish?
问题描述
我想知道有多少整数有''数组''。 (int *数组)
或者字符有''数组''(字符*数组)
等...
所以......
while(array){...} ????
while(array!= EOF){...} ????
while(array!= NULL){...} ???
(我想使用最小可能的变量)
Olaf" El Blanco" opined:
我想知道有多少整数有''数组''。 (int * array)
或者字符有''数组''(字符*数组)
等...
所以...
while(数组) {...} ????
while(array!= EOF){...} ????
while(array!= NULL){...} ???
(我想使用最小的可能变量)
一般情况下你不能。
如果您的数组以特定值终止,比如C中的字符串为'/'0',那么您可以遍历它们直到达到该值。
如果它们被声明为:
int array [SOME_SIZE];
你可以得到这样的大小:
sizeof数组/ sizeof数组[0](== SOME_SIZE)
C中没有其他方法。
-
BR,Vladimir
这些年来,我已经如此敏锐地发展了我的似曾相识感现在
我能记住之前发生的事情......
" Vladimir S. Oka" <无**** @ btopenworld.com> escribióenel mensaje
新闻:WM ****************************** @ bt.com。 ..Olaf" El Blanco" opined:
我想知道有多少整数有''数组''。 (int * array)
或者字符有''数组''(字符*数组)
等...
所以...
while(数组) {...} ????
while(array!= EOF){...} ????
while(array!= NULL){...} ???
(我想使用最小可能的变量)
一般情况下你不能。
如果您的阵列是以特定值终止,如字符串在C中带有''\ 0'',你可以遍历它们直到达到该值。
如果它们被声明为:
int数组[SOME_SIZE];
不,声明如下:
int * array
所以,如果我要打印所有数组,我不能???
Olaf El Blanco写道:我想知道有多少整数有''数组''。 (int * array)
或者字符有''数组''(字符*数组)
等...
所以...
while(数组) {...} ????
while(array!= EOF){...} ????
while(array!= NULL){...} ???
(我想使用最小可能的变量)
您的问题不明确。如果你想知道固定大小数组中元素的数量
那么sizeof(array)/ sizeof(array [0])应该得到
的答案。如果它是一个动态分配的数组,那么你将不得不
跟踪它的大小,(因此它拥有的元素数量)
你自己。例如,如果*数组的类型为long指针,并且它的初始化指向N个字节数组的开头,那么数组
指向N / sizeof *你可以访问的数组元素。
希望这有所帮助。
I want to know how many integers has ''array''. (int *array)
Or chars has ''array'' (char *array)
Etc...
So...
while (array) { ... } ????
while (array != EOF) {...} ????
while (array != NULL) {...} ???
(I want to use the minimun possible variables)解决方案Olaf "El Blanco" opined:
I want to know how many integers has ''array''. (int *array)
Or chars has ''array'' (char *array)
Etc...
So...
while (array) { ... } ????
while (array != EOF) {...} ????
while (array != NULL) {...} ???
(I want to use the minimun possible variables)
You can''t, in general case.
If your arrays are terminated with a specific value, like strings are
with ''\0'' in C, you could traverse them until reaching that value.
If they were declared like:
int array[SOME_SIZE];
you could get their size like this:
sizeof array / sizeof array[0] ( == SOME_SIZE)
There is no other way in C.
--
BR, Vladimir
Over the years, I''ve developed my sense of deja vu so acutely that now
I can remember things that *have* happened before ...
"Vladimir S. Oka" <no****@btopenworld.com> escribió en el mensaje
news:WM******************************@bt.com...Olaf "El Blanco" opined:I want to know how many integers has ''array''. (int *array)
Or chars has ''array'' (char *array)
Etc...
So...
while (array) { ... } ????
while (array != EOF) {...} ????
while (array != NULL) {...} ???
(I want to use the minimun possible variables)
You can''t, in general case.
If your arrays are terminated with a specific value, like strings are
with ''\0'' in C, you could traverse them until reaching that value.
If they were declared like:
int array[SOME_SIZE];
No, is declared like:
int *array
So, If I want to print all the array, I can''t???
Olaf El Blanco wrote:I want to know how many integers has ''array''. (int *array)
Or chars has ''array'' (char *array)
Etc...
So...
while (array) { ... } ????
while (array != EOF) {...} ????
while (array != NULL) {...} ???
(I want to use the minimun possible variables)
Your question is not clear. If you want to know the number of elements
in a fixed size array then sizeof(array)/sizeof(array[0]) should yield
the answer. If it a dynamically allocated array, then you''ll have to
keep track of it''s size, (and hence the number of elements it has)
yourself. For example if *array is of type pointer to long and it''s
initialised to point to the start of an array of N bytes then array
points to N/sizeof *array elements that you can access.
Hope this helps somewhat.
这篇关于数组 - 他们完成的地方?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!