Returning array from function in C
以下为了通俗易懂,使用意译。
I've here very intersting discussion about the best and common ways to return an array from a function..
我最近很热衷于讨论从函数返回数组的最佳及常用方法
Some solutions to use output parameter and copy the value of the array into the value of this output parameter array.
Other solution to pass an array and use it inside the function.
一些解决方案是使用数组作为输出参数,并将值复制到这个参数。
Others to allocate the array inside the function and return refrence to it, but the caller have to free it once done.
其他一些则是在函数内部申请空间来存放数组,并将引用(此处应该是指针地址)返回给主调函数,但调用者必须在使用完以后释放。
Others to return a struct contains this pointer...
再者就是返回一个结构,包含这个数组的指针。
以下解决方案与诸君分享:
Please enjoy; stackoverflow.com/questions/8865982/return-array-from-function-in-c
const char numbers[] = "0123456789abcdef"; void getBase(int n, int b, char* str)
{
const size_t SIZE = ;
int digits=SIZE;
while (n > )
{
int t = n%b;
n/=b;
str[--digits] = numbers[t];
} int length = SIZE - digits; memmove(str,str + digits,length);
str[length] = '\0';
}
You just have to make sure that your str
is large enough to avoid an array-overrun.
int main(){ char str[]; getBase(,,str); printf(str); return ;
}
返回结果
最新文章
- Python操作MySQL
- Quartz2D简介
- [BZOJ2959]长跑——新技能:LCT+缩圈
- python模拟shell
- 基于easyui的验证扩展
- git 一个文件还原到某个提交的commit
- opencv 在工业中的应用:圆孔定位
- linux 小技巧总结
- O-C相关-06:对象与对象的关系
- java开发规范总结_命名规范
- SRAM与SDRAM的比较(转)
- java开发中遇到的问题及解决方法(持续更新)
- v​s​快​捷​键
- TreeSet两种比较
- C# /VB.NET 创建PDF项目符号列表和多级编号列表
- Error filterStart的问题
- 在phpstudy中安装并使用ThinkPHP 5
- Vue.js的简介
- json数据格式说明
- bzoj1497 最大获利(最大权闭合子图)