Hello All 我有一个2D数组, char arr [4] [10] = {" Hello", world,test,hi}; 如何找到数组的strlen 我期待数组长度为4 感谢Adv
Hello All I have a 2D array, char arr[4][10] = {"Hello","world","test","hi"}; how can find the strlen of the array am expecting array length as "4" Thanks in Adv
推荐答案" Imran" < IM ****** @ in.bosch>在消息中写道 news:cf ********** @ ns1.fe.internet.bosch ... "Imran" <im******@in.bosch> wrote in message news:cf**********@ns1.fe.internet.bosch... Hello All 我有一个2D数组, 如何找到阵列的strlen 我希望数组长度为4 感谢Adv Hello All I have a 2D array, char arr[4][10] = {"Hello","world","test","hi"}; how can find the strlen of the array am expecting array length as "4" Thanks in Adv
您的预期答案表明您确实在尝试确定多维数组的第一个维度。 strlen函数没有这样做。这样做。在什么情况下你想确定这个?如果你在函数内部是,那么你可能必须将第一个维度传递给 函数。例如: void foo(char(* arr)[10],int size){ for(int i = 0; i< size ; ++ i){ std :: cout<< arr [i]<< std :: endl; } } int main(){ char arr [4] [10] = {" Hello",world,test,hi}; foo(arr,sizeof(arr)/ sizeof(arr [ 0])); 返回0; } - David Hilsee
Your expected answer indicates that you are really trying to determine the first dimension of a multi-dimensional array. The strlen function does not do this. In what context are you trying to determine this? If you''re inside a function, then you may have to pass the first dimension to that function. For example: void foo(char (*arr)[10], int size) { for ( int i = 0; i < size; ++i ) { std::cout << arr[i] << std::endl; } } int main() { char arr[4][10] = {"Hello","world","test","hi"}; foo(arr,sizeof(arr)/sizeof(arr[0])); return 0; } -- David Hilsee
" Imran" < IM ****** @ in.bosch>在消息新闻中写道:< cf ********** @ ns1.fe.internet.bosch> ... "Imran" <im******@in.bosch> wrote in message news:<cf**********@ns1.fe.internet.bosch>... Hello All 我有一个2D数组, char arr [4] [10] = {" Hello",world,test,hi}; <如何找到数组的strlen 我希望数组长度为4 Hello All I have a 2D array, char arr[4][10] = {"Hello","world","test","hi"}; how can find the strlen of the array am expecting array length as "4"
当你''时有一种可能性真正使用数组(即不是一个 指针)就像: #define elements(x)((sizeof(x)/ sizeof (x [0])) 请记住,数组会随时衰减到指针 它作为参数传递,所以这实际上只能应用于全局,或者在定义数组的函数内部。 - 后来, 杰瑞。 宇宙是自己想象的虚构。
One possibility when you''re truly working with an array (i.e. NOT a pointer) is something like: #define elements(x) ((sizeof(x)/sizeof(x[0])) Keep in mind, however, that an array will decay to a pointer anytime it is passed as a parameter, so this can really only be applied to globals, or else inside of the function where the array is defined. -- Later, Jerry. The universe is a figment of its own imagination.
更多推荐
strlen的
发布评论