Possible Duplicate:
length of array in function argument可能重复:函数参数中的数组长度
My array size is 5. For example:
我的数组大小为5.例如:
arrCustId[5]
How can I know how many Customer IDs are already present in my array? In short how to find length of array?
我怎么知道我的阵列中已存在多少个客户ID?总之如何找到数组的长度?
3 个解决方案
#1
26
If the array is statically allocated, use sizeof(array) / sizeof(array[0])
如果数组是静态分配的,请使用sizeof(array)/ sizeof(array [0])
If it's dynamically allocated, though, unfortunately you're out of luck as this trick will always return sizeof(pointer_type)/sizeof(array[0])
(which will be 4 on a 32 bit system with char*s) You could either a) keep a #define (or const) constant, or b) keep a variable, however.
如果它是动态分配的,不幸的是,你运气不好,因为这个技巧总会返回sizeof(pointer_type)/ sizeof(array [0])(在带有char * s的32位系统上将是4)你可以a)保持#define(或const)常数,或b)保持变量。
#2
12
Do you mean how long is the array itself, or how many customerids are in it?
你的意思是阵列本身有多长,或者有多少顾客?
Because the answer to the first question is easy: 5 (or if you don't want to hard-code it, Ben Stott's answer).
因为第一个问题的答案很简单:5(或者如果你不想硬编码,Ben Stott的回答)。
But the answer to the other question cannot be automatically determined. Presumably you have allocated an array of length 5, but will initially have 0 customer IDs in there, and will put them in one at a time, and your question is, "how many customer IDs have I put into the array?"
但是其他问题的答案无法自动确定。据推测,您已经分配了一个长度为5的数组,但最初会有0个客户ID,并且会将它们一次放入一个,您的问题是“我将多少客户ID放入数组?”
C can't tell you this. You will need to keep a separate variable, int numCustIds
(for example). Every time you put a customer ID into the array, increment that variable. Then you can tell how many you have put in.
C不能告诉你这个。您需要保留一个单独的变量int numCustIds(例如)。每次将客户ID放入数组时,请递增该变量。然后你就可以知道你投入了多少。
#3
-8
I'm not sure that i know exactly what you mean.
我不确定我到底知道你的意思。
But to get the length of an initialized array,
但要获得初始化数组的长度,
doesn't strlen(string) work ??
不strlen(字符串)工作?
#1
26
If the array is statically allocated, use sizeof(array) / sizeof(array[0])
如果数组是静态分配的,请使用sizeof(array)/ sizeof(array [0])
If it's dynamically allocated, though, unfortunately you're out of luck as this trick will always return sizeof(pointer_type)/sizeof(array[0])
(which will be 4 on a 32 bit system with char*s) You could either a) keep a #define (or const) constant, or b) keep a variable, however.
如果它是动态分配的,不幸的是,你运气不好,因为这个技巧总会返回sizeof(pointer_type)/ sizeof(array [0])(在带有char * s的32位系统上将是4)你可以a)保持#define(或const)常数,或b)保持变量。
#2
12
Do you mean how long is the array itself, or how many customerids are in it?
你的意思是阵列本身有多长,或者有多少顾客?
Because the answer to the first question is easy: 5 (or if you don't want to hard-code it, Ben Stott's answer).
因为第一个问题的答案很简单:5(或者如果你不想硬编码,Ben Stott的回答)。
But the answer to the other question cannot be automatically determined. Presumably you have allocated an array of length 5, but will initially have 0 customer IDs in there, and will put them in one at a time, and your question is, "how many customer IDs have I put into the array?"
但是其他问题的答案无法自动确定。据推测,您已经分配了一个长度为5的数组,但最初会有0个客户ID,并且会将它们一次放入一个,您的问题是“我将多少客户ID放入数组?”
C can't tell you this. You will need to keep a separate variable, int numCustIds
(for example). Every time you put a customer ID into the array, increment that variable. Then you can tell how many you have put in.
C不能告诉你这个。您需要保留一个单独的变量int numCustIds(例如)。每次将客户ID放入数组时,请递增该变量。然后你就可以知道你投入了多少。
#3
-8
I'm not sure that i know exactly what you mean.
我不确定我到底知道你的意思。
But to get the length of an initialized array,
但要获得初始化数组的长度,
doesn't strlen(string) work ??
不strlen(字符串)工作?