博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C Array length function problem - C / C++
阅读量:6860 次
发布时间:2019-06-26

本文共 4111 字,大约阅读时间需要 13 分钟。

C Array length function problem
2n2is5
P: 2
   
2n2is5
I've been experimenting with arrays, and I have just found the simple way to find the length of an array. I attempted to make this a function with the specific array as the only argument. It didn't work. I thought it was bad coding; but, after a while, I got fed up and put the same code in both the main() and the function. Like so
Expand|Select|Wrap|Line Numbers
    int getLength(int *myarray);
    int main(void)
    {
        int myarray[]={5,4,3,2,1};
        int size1=(sizeof(myarray) / sizeof(myarray[0]));
        int size2=int_getLength(myarray);
        printf("%d and %d should be the same",size1,size2);
        return 0;
    }
    int getLength(int *myarray)
    {
        return (sizeof(myarray) / sizeof(myarray[0]));
    }
    
size1,main, prints to be 5, which is the correct length.
size2, function, prints 1.
I don't know why this is happening. I have also tried using (int myarray[])as the parameter, but the output is the same.
My logical assumption would be that the function is only passing in the first element of the array or it could do something with obtaining the length. Can someone please explain why this is happening.
Post Reply
Feb 9 '08
#1
Share this Question
Share on Google+
3 Replies
Ads by Google
免费下载《一周商务英语》
www.englishtown.com/EF
期待事业更上一层楼?从此沟通更轻松! 助你熟练掌握商务英语交流术。
gpraghuram
Expert 100+
P: 1,271
   
gpraghuram
I've been experimenting with arrays, and I have just found the simple way to find the length of an array. I attempted to make this a function with the specific array as the only argument. It didn't work. I thought it was bad coding; but, after a while, I got fed up and put the same code in both the main() and the function. Like so
Expand|Select|Wrap|Line Numbers
    int getLength(int *myarray);
    int main(void)
    {
        int myarray[]={5,4,3,2,1};
        int size1=(sizeof(myarray) / sizeof(myarray[0]));
        int size2=int_getLength(myarray);
        printf("%d and %d should be the same",size1,size2);
        return 0;
    }
    int getLength(int *myarray)
    {
        return (sizeof(myarray) / sizeof(myarray[0]));
    }
    
size1,main, prints to be 5, which is the correct length.
size2, function, prints 1.
I don't know why this is happening. I have also tried using (int myarray[])as the parameter, but the output is the same.
My logical assumption would be that the function is only passing in the first element of the array or it could do something with obtaining the length. Can someone please explain why this is happening.
When single dimension array is passed as a parameter to a function then it is passed as a pointer.
Thats why u are getting the size like that
Raghuram
Feb 9 '08
#2
reply
weaknessforcats
Expert Mod 5K+
P: 6,832
   
weaknessforcats
The real problem is a thing called decay of array.
That is, when an array is passed to a function, all that is passed is the address of element 0. So the function gets and address and not the entire array. From the perspective of the function, the array has disappeared and all the function sees is a pointee to a single vaiable. The function must assume there is an array.
Then, the sizeof only tells you the size of the variable on the local stack. In this case, the size of the address, which is most likely 4.
So, don't use that sizeof trick to get the number of array elements. Instead define a const int that is set to the number of elements and pass that along withe the array name to the function.
Feb 9 '08
#3
reply
2n2is5
P: 2
   
2n2is5
The real problem is a thing called decay of array.
That is, when an array is passed to a function, all that is passed is the address of element 0. So the function gets and address and not the entire array. From the perspective of the function, the array has disappeared and all the function sees is a pointee to a single vaiable. The function must assume there is an array.
Then, the sizeof only tells you the size of the variable on the local stack. In this case, the size of the address, which is most likely 4.
So, don't use that sizeof trick to get the number of array elements. Instead define a const int that is set to the number of elements and pass that along withe the array name to the function.
I had assumed that was the only way, Thanks.

转载地址:http://hfxyl.baihongyu.com/

你可能感兴趣的文章
指尖上的电商---(10)SolrAdmin中加入多核
查看>>
CCEditBox/CCEditBoxImplAndroid
查看>>
TCP/IP协议栈--IP首部选项字段的分析
查看>>
Kubuntu 初始配置
查看>>
python中列表和元组的操作(结尾格式化输出小福利)
查看>>
用过的一些服务器集成软件
查看>>
一键拨打
查看>>
20120522:ERROR - ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务
查看>>
Maven构建war项目添加版本号
查看>>
更新 手淘 flexible 布局 rem 单位适配问题
查看>>
第三次作业
查看>>
新浪微博登录接口实例
查看>>
wcf技术剖析_会话
查看>>
AngularJS 指令的 Scope (作用域)
查看>>
gitlab的使用
查看>>
iOS 生成本地验证码
查看>>
找不到 javax.servlet.http.HttpServletResponse 和 javax.servlet.http.HttpServletRequest 问题解决...
查看>>
Flip Game(枚举)
查看>>
WebWorker与WebSocket实现前端消息总线
查看>>
Selector
查看>>