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

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

返回结果


Returning array from function in C的更多相关文章

  1. Object、Function、String、Array原生对象扩展方法

    JavaScript原生对象的api有些情况下使用并不方便,考虑扩展基于Object.Function.String.Array扩展,参考了prototype.js的部分实现,做了提取和修改,分享下: ...

  2. [转] iOS ABI Function Call Guide

    source: apple ARMv6 Function Calling Conventions When functions (routines) call other functions (sub ...

  3. jQuery源码06-jQuery = function(){};给JQ对象,添加一些方法和属性,extend : JQ的继承方法,jQuery.extend()

    /*! * Includes Sizzle.js 选择器,独立的库 * http://sizzlejs.com/ */ (function( window, undefined ) { //" ...

  4. javascript中的Function和Object

    写的很好,理解了很多,特此转发记录 转自:http://blog.csdn.net/tom_221x/archive/2010/02/22/5316675.aspx 在JavaScript中所有的对象 ...

  5. JavaScript Array map() 方法

    语法: array.map(function(currentValue,index,arr), thisValue) currentValue:必须.当前元素的值index:可选.当期元素的索引值ar ...

  6. JS原型的问题Object和Function到底是什么关系

    var F = function(){}; Objcert.prototype.a = function(){}; Function.prototype.b = function(){}; F 既能访 ...

  7. js中Array的一些扩展

    IE下很多Array的方法都不被支持.每次都要写.所以记下来,以免忘记: 以下是对Array的一些扩展,在FF ,google 下是不需要加的. /** * 方法Array.filter(functi ...

  8. 回文数组(Rotate Array (JS))

    旋转一个数组. function rotate(array,n){ var l =array.length,a=array.map(function(x){return x}),arr=[]; n=n ...

  9. polyfill之javascript函数的兼容写法——Array篇

    1. Array.isArray(obj) if (!Array.isArray) { Array.isArray = function(arg) { return Object.prototype. ...

随机推荐

  1. [Nginx]子目录反向代理kibana并添加basic认证

    背景 服务器ip:192.168.1.2 安装软件 nginx kibana(默认端口5601) 实现方案:访问http://192.168.1.2/kibana 即可访问到kibana后端,同时需要 ...

  2. 11.tensorboard网络结构

    import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data # 载入数据集 mnist = i ...

  3. (web)个人项目(挖宝网)

    前台:使用bootstrap. 后台:使用layui. 数据库:mysql 下载地址:https://github.com/oukele/wabaowangDemo(数据库文件忘记上传,我在展示功能结 ...

  4. 高并发下的Nginx优化

    高并发下的Nginx优化 2014-08-08 13:30 mood Nginx    过去谈过一些关于Nginx的常见问题; 其中有一些是关于如何优化Nginx. 很多Nginx新用户是从Apach ...

  5. 【原】spring+mybatis下sqlSession.delete和insert返回值-2147482646问题

    这是由于spring-beans.xml中的batch批处理配置所导致的,注释掉BATCH配置的代码就可以返回1了: <bean id="sqlSessionFactory" ...

  6. Subarray Sorting (线段树)

    题意:给你两个长度为 n 的序列 a 和 b , 可以对 a 进行 操作: 选择一段区间[ l, r ] ,使得序列a 在这段区间里 按升序排序. 可以对a 进行任意多次操作,问 a是否有可能变成b序 ...

  7. Hive运行原理--JOIN

    对于 JOIN 操作: INSERT OVERWRITE TABLE pv_users SELECT pv.pageid, u.age FROM page_view pv JOIN user u ON ...

  8. 阿里云maven镜像

    <mirror> <id>nexus-aliyun</id> <mirrorOf>*</mirrorOf> <name>Nexu ...

  9. 邻居子系统 之 邻居表的初始化neigh_table_init

    概述 邻居子系统支持多种实现,例如ARP,ND等,这些实现需要在其初始化的时候,调用neigh_table_init将邻居表项添加到全局邻居子系统数组中,并对实例中的字段(如hash,定时器等)进行相 ...

  10. Linux 如何查看端口与进程占用情况

    1 lsof -i:port  查看端口使用情况 lsof -i 如果出现 command not found,直接yum install lsof即可. (1) lsof -i lsof -i 用以 ...