以下为了通俗易懂,使用意译。

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 ;
}

返回结果


最新文章

  1. Python操作MySQL
  2. Quartz2D简介
  3. [BZOJ2959]长跑——新技能:LCT+缩圈
  4. python模拟shell
  5. 基于easyui的验证扩展
  6. git 一个文件还原到某个提交的commit
  7. opencv 在工业中的应用:圆孔定位
  8. linux 小技巧总结
  9. O-C相关-06:对象与对象的关系
  10. java开发规范总结_命名规范
  11. SRAM与SDRAM的比较(转)
  12. java开发中遇到的问题及解决方法(持续更新)
  13. v​s​快​捷​键
  14. TreeSet两种比较
  15. C# /VB.NET 创建PDF项目符号列表和多级编号列表
  16. Error filterStart的问题
  17. 在phpstudy中安装并使用ThinkPHP 5
  18. Vue.js的简介
  19. json数据格式说明
  20. bzoj1497 最大获利(最大权闭合子图)

热门文章

  1. sql 脚本过大
  2. CentOS7 xrdp 安装和设置
  3. 牛客小白月赛12 D 月月给华华出题 (欧拉函数,数论,线筛)
  4. (五) 结构化查询语言SQL——3
  5. mac 环境下Android 反编译源码
  6. React组件:Dragact 0.1.4发布
  7. 深入理解JavaScript执行上下文、函数堆栈、提升的概念
  8. BZOJ 2119: 股市的预测 (Hash / 后缀数组 + st表)
  9. 一些C++编码规范
  10. Git 如何针对项目修改本地提交提交人的信息